Exception when parsing RFC1123 date with HttpServletRequest#getDateHeader() - java

I am trying to parse the HTTP header Date: Tue, 15 Nov 1994 08:12:31 GMT for an HttpServletRequest with a call to request.getDateHeader("Date") but get the following exception:
java.lang.NumberFormatException: Tue, 15 Nov 1994 08:12:31 GMT
at org.mortbay.io.BufferUtil.toLong(BufferUtil.java:106)
at org.mortbay.jetty.HttpFields$Field.getLongValue(HttpFields.java:1479)
at org.mortbay.jetty.HttpFields.getLongField(HttpFields.java:720)
at org.mortbay.jetty.Request.getIntHeader(Request.java:728)
at javax.servlet.http.HttpServletRequestWrapper.getIntHeader(HttpServletRequestWrapper.java:106)
at javax.servlet.http.HttpServletRequestWrapper.getIntHeader(HttpServletRequestWrapper.java:106)
at com.google.appengine.tools.development.ResponseRewriterFilter$RequestWrapper.getDateHeader(ResponseRewriterFilter.java:417)
at com.example.ExampleServlet.doPost(ExampleServlet.java:93)
What am I doing wrong? The sample date is taken directly from the HTTP specification, so I would expect it to be valid under RFC 1123. The servlet is part of an App Engine app (on development server).

This is a known bug in the app engine library. Here is the getDateHeader function at issue, and it clearly calls super.getIntHeader(). It doesn't look like Google is in a hurry to fix it, unfortunately.

Related

AWS lambda :Malformed Lambda proxy response

This is a typical issue while Lambda sitting behind API gateway. I've googled almost all the posts but still can't not figure out why it fails.
I've read the instruction on what the response should be look like
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
It is looking for
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
"body": "..."
}
Here is the log of my test from API gateway console (I have double checked make sure the name of fields are exactly the same)
Thu Mar 19 10:07:58 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"body\":\"RESULTS\",\"isBase64Encoded\":false}"
Thu Mar 19 10:07:58 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 10:07:58 UTC 2020 : Method completed with status: 502
Can anyone help?
EDIT
I've removed "body" from response and still get the same error. The following is the actual log
Thu Mar 19 23:29:54 UTC 2020 : Received response. Status: 200, Integration latency: 24 ms
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response headers: {Date=Thu, 19 Mar 2020 23:29:54 GMT, Content-Type=application/json, Content-Length=48, Connection=keep-alive, x-amzn-RequestId=f2c2c752-a5e0-45e4-9ff0-d91826b51c7b, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5e740072-46cee9045a56e04b8023816d;sampled=0}
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Mar 19 23:29:54 UTC 2020 : Method completed with status: 502
The actual JSON response catptured in java app & lambda is
{"statusCode":200,"isBase64Encoded":false}
After comparing to Node.js example, seems that it is to do with the handler.
When using a RequestHandler, the response is a String
public class MyLambda implements RequestHandler<Object, String> {
#Override
public String handleRequest(Object input, Context context) {
From API gateway, The response is showing as follows, note that the whole response is a String, and API gateway complains
Thu Mar 19 23:29:54 UTC 2020 : Endpoint response body before transformations: "{\"statusCode\":200,\"isBase64Encoded\":false}"
Thu Mar 19 23:29:54 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Changed that to RequestStreamHandler , the error is gone
public class APIgatewayTest implements RequestStreamHandler {
#Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
From API gateway, the response is as follows
Thu Apr 09 09:13:08 UTC 2020 : Endpoint response body before transformations: {"statusCode":200,"body":"\"Hello from Lambda!\""}
Thu Apr 09 09:13:08 UTC 2020 : Method response body after transformations: "Hello from Lambda!"

Java Scribe library Twitter OAuth1 request token 401 unauthorized

I have been trying to use different Java OAuth libraries to get request and access token from Twitter API. Finally, I have come to use Scribe as one of the most stable libraries for this matter. However, I have not been able to get this to work with Twitter on the first step which is getting the access token:
twitterOAuthService = new ServiceBuilder(twitterOauthConsumerKey)
.apiSecret(twitterOauthConsumerKey).debug().callback(callback).build(
TwitterApi.instance());
OAuth1RequestToken requestToken = twitterOAuthService.getRequestToken();
It throws the following exception:
Response{code=401, message='Authorization Required', body='null', headers={date=Tue, 05 Nov 2019 07:06:34 GMT, null=HTTP/1.1 401 Authorization Required, server=tsa_l, content-length=64, expires=Tue, 31 Mar 1981 05:00:00 GMT, x-response-time=166, x-frame-options=SAMEORIGIN, www-authenticate=OAuth realm="https://api.twitter.com", x-transaction=00e2c19300b2d1ab, strict-transport-security=max-age=631138519, pragma=no-cache, set-cookie=guest_id=v1%3A157293759473549885; Max-Age=63072000; Expires=Thu, 4 Nov 2021 07:06:34 GMT; Path=/; Domain=.twitter.com, last-modified=Tue, 05 Nov 2019 07:06:34 GMT, x-xss-protection=0, x-content-type-options=nosniff, content-disposition=attachment; filename=json.json, x-connection-hash=badf004485a6d713a29ed3f90de9e981, x-twitter-response-tags=BouncerCompliant, content-type=application/json; charset=utf-8, cache-control=no-cache, no-store, must-revalidate, pre-check=0, post-check=0, status=401 Unauthorized}}
I can see that the Authorization header is set properly:
Authorization -> OAuth oauth_callback="http%253A%252F%252F127.0.0.1%253A3000%252Fsettings", oauth_consumer_key="8hupaLbXA2s792EzU8DIE6K", oauth_nonce="2929183554", oauth_signature="s%2BRMAninTESJqJg92BuEA8h3E%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1572936780", oauth_version="1.0"
I have tested the below approaches as they were suggested in different places:
Ensure Authorization header is sorted alphabetically!
Ensure that the timestamp is set properly
Ensure that the consumer secret and consumer key are set properly
Ensure that the encoding of oauth_signature (percent encoding) and call_back is set properly
I have run out of any other scenarios and I am not even sure if I can use Scribe library for Twitter. I would appreciate it if someone could help me to address this issue.

SOAP UI Restful : An error occurred [Illegal character in path at index

I am currently developing a Web Service RESTFUL.
To test the Web Service, I'm using SOAP UI.
While performing testing, I got the error Illegal character in path.
May I know what is the cause of the error? How to fix the issue?
Below is the detail of the problem.
Error Log:
Wed Nov 28 17:05:26 SGT 2018:ERROR:java.net.URISyntaxException: Illegal character in path at index 62: http://127.0.0.1:8080/AdaptiveAuthWSApps/rest/AdaptiveService/{uuid}/{browserinfo}/{ipint}/{lat}/{longitude}/{sessionid}/{spid}/{tr}/{jsnum}/{fingerprint}/{methodset}
java.net.URISyntaxException: Illegal character in path at index 62: http://127.0.0.1:8080/AdaptiveAuthWSApps/rest/AdaptiveService/{uuid}/{browserinfo}/{ipint}/{lat}/{longitude}/{sessionid}/{spid}/{tr}/{jsnum}/{fingerprint}/{methodset}
SOAP UI Log:
Wed Nov 28 17:05:26 SGT 2018:ERROR:An error occurred [Illegal character in path at index 62: http://127.0.0.1:8080/AdaptiveAuthWSApps/rest/AdaptiveService/{uuid}/{browserinfo}/{ipint}/{lat}/{longitude}/{sessionid}/{spid}/{tr}/{jsnum}/{fingerprint}/{methodset}], see error log for details
Wed Nov 28 17:05:26 SGT 2018:DEBUG:Attempt 1 to execute request
Wed Nov 28 17:05:26 SGT 2018:DEBUG:Sending request: POST /?uuid=iz23456&browserinfo=Mozilla%2F8.0%20%28Windows%20NT%206.1%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F49.0.2623.112%20Safari%2F537.36&ipint=16885247&lat=1&longitude=0&sessionid=12w3e4&spid=https%3A%2F%2Fuap%2Fowa&tr=10&jsnum=2&fingerprint=yh8u87&methodset=1%2C1 HTTP/1.1
Wed Nov 28 17:05:26 SGT 2018:DEBUG:Receiving response: HTTP/1.1 200
Wed Nov 28 17:05:26 SGT 2018:DEBUG:Connection can be kept alive indefinitely
Below is the WADL that I used when creating the Test Suite in SOAP UI.
http://127.0.0.1:8080/AdaptiveAuthWSApps/rest/application.wadl
SOAP UI parameter initialize:
I believe the problem is the value of your uuid parameter.
A uuid should consist solely of hexadecimal characters. That is 0-9 and a-f (and also the "-" of course)
You've written iz123456, and neither "i" or "z" are valid.
If the uuid also should be an actual uuid, then it should look something like this: 13a1a079-4a39-4625-91a4-ddbf09c2c324
Try copy/pasting this uuid into your uuid parameter, and run it again.
It looks like you're getting your parameter styles mixed up. If you define a style of QUERY, the key/value pairs are added after the question mark in the URL, not added to the resource path. Using {uuid}/{browserinfo}/... is a TEMPLATE style.
So, remove {uuid}/{browserinfo}/... from your resource path and just leave the key/value pairs after the question mark.

SOAP UI displays faultstring even after error 500 is received. need to view the same in JAVA

Here's the story:
Sending a request from SOAPUI (5.2.1) gives me either correct response or, in case of invalid parameters, the error in form of xml.
Looking at SoapUI log this is what i see:
Wed Jan 18 16:32:39 EST 2017:DEBUG:Attempt 1 to execute request
Wed Jan 18 16:32:39 EST 2017:DEBUG:Sending request: POST/XBC/services/TranslateGeometryService HTTP/1.1
Wed Jan 18 16:32:48 EST 2017:DEBUG:Receiving response: HTTP/1.1 401 Unauthorized
Wed Jan 18 16:32:48 EST 2017:DEBUG:Connection can be kept alive for 10000 MILLISECONDS
Wed Jan 18 16:32:48 EST 2017:DEBUG:Target requested authentication
Wed Jan 18 16:32:48 EST 2017:DEBUG:Authorization challenge processed
Wed Jan 18 16:32:48 EST 2017:DEBUG:Authentication scope: BASIC 'Spring Security Application'#mywebservice:80
Wed Jan 18 16:32:48 EST 2017:INFO:mywebservice:80 requires authentication with the realm 'Spring Security Application'
Wed Jan 18 16:32:48 EST 2017:DEBUG:Found credentials
Wed Jan 18 16:32:48 EST 2017:DEBUG:Attempt 2 to execute request
Wed Jan 18 16:32:48 EST 2017:DEBUG:Sending request: POST/XBC/services/TranslateGeometryService HTTP/1.1
Wed Jan 18 16:33:14 EST 2017:DEBUG:Receiving response: HTTP/1.1 500 Internal Server Error
Wed Jan 18 16:33:14 EST 2017:DEBUG:Connection shut down
Wed Jan 18 16:33:14 EST 2017:INFO:Got response for [TranslateGeometrySoap11.getLocationFromTramPoleTrackOffset:Request 1] in 35794ms (312 bytes)
(Note that those 312 bytes come after error 500 and connection shut down messages)
Response window, at the same time, in SOAPUI shows this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring xml:lang="en">Error: No Track exists with Track Code: CHS-CSK-UP-TW</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
This is perfect and works as i expect it to work.
Now for my problem that is seriously driving me mad.
When i attempt to send exactly the same request from my java code that is supposed to return the error above, i receive IOException that says:
Server returned HTTP response code: 500 for URL: http://mywebservice/XBC/services/TranslateGeometryService
So, pretty much same as what SOAPUI shows in its log.
But...
No matter what i do in my code i cannot get that message in faultstring that SOAPUI displays: "No Track exists with Track Code: CHS-CSK-UP-TW".
How is SOAPUI doing it after it receives error 500? Or, more importantly - how do i get it in JAVA after receiving error 500?
Tried playing with SoapFaultException class and SoapFault, but i could not find anything more anywhere.
Thanks to anyone who tries to answer this question.
Cheers,
Pierre
The next code in Java handler uses SOAPMessageContext TO detect SOAP fault and extract fault string from SOAPBody:
public bool handleFault(SOAPMessageContext context)
{
try {
if (context.getMessage().getSOAPBody().hasFault()) {
SOAPFault fa = context.getMessage().getSOAPBody().getFault();
System.err.println(fa.getFaultCode());
System.err.println(fa.getFaultString());
}
return true;
} catch (SOAPException ex) {
System.err.println("Fault parsing Exception:" + ex.getMessage());
return true;
}
return false;
}

Twitter4j search get only few fields

Twitter4j search returns results with many fields.
ex:
StatusJSONImpl{createdAt=Fri Jan 09 19:56:31 IST 2015, id=553558441482924033, text='Avast Free Antivirus 2015 [Download]: Avast Free Antivirus 2015 [Download] by AVAST Software s.r.o. Platform:… http://t.co/0zLPutC1PU', source='dlvr.it', isTruncated=false, inReplyToStatusId=-1, inReplyToUserId=-1, isFavorited=false, isRetweeted=false, favoriteCount=0, inReplyToScreenName='null', geoLocation=null, place=null, retweetCount=0, isPossiblySensitive=false, isoLanguageCode='en', lang='en', contributorsIDs=[], retweetedStatus=null, userMentionEntities=[], urlEntities=[URLEntityJSONImpl{url='http://t.co/0zLPutC1PU', expandedURL='http://amzn.to/1q69X9Z', displayURL='amzn.to/1q69X9Z'}], hashtagEntities=[], mediaEntities=[], symbolEntities=[], currentUserRetweetId=-1, user=UserJSONImpl{id=2792966010, name='Jessica', screenName='JessicaN372', location='US', description='Jessica', isContributorsEnabled=false, profileImageUrl='http://pbs.twimg.com/profile_images/508946722307846144/E5Bi70E8_normal.jpeg', profileImageUrlHttps='https://pbs.twimg.com/profile_images/508946722307846144/E5Bi70E8_normal.jpeg', url='null', isProtected=false, followersCount=128, status=null, profileBackgroundColor='C0DEED', profileTextColor='333333', profileLinkColor='0084B4', profileSidebarFillColor='DDEEF6', profileSidebarBorderColor='C0DEED', profileUseBackgroundImage=true, showAllInlineMedia=false, friendsCount=0, createdAt=Sat Sep 06 08:13:33 IST 2014, favouritesCount=0, utcOffset=-1, timeZone='null', profileBackgroundImageUrl='http://abs.twimg.com/images/themes/theme1/bg.png', profileBackgroundImageUrlHttps='https://abs.twimg.com/images/themes/theme1/bg.png', profileBackgroundTiled=false, lang='en', statusesCount=148653, isGeoEnabled=false, isVerified=false, translator=false, listedCount=2, isFollowRequestSent=false}}
Where I only require text and links.
How to request only certain fields
It's not possible to request certain fields as the Twitter Search API doesn't provide this functionality.
Instead you will need to extract what you are interested in from the Status yourself.
In your case you can use status.getText() and status.getUrlEntities() to get what you want.

Categories