HTTP Get fails to retrieve jpeg hosted on Amazon EC2 - java

I've developed a java web app that uses a jetty server to host images publicly (such as the image http://52.23.50.128:8080/reports/94109_17_03_21_14.jpeg), and then uses the twilio api to grab that image from a provided link, and shoot the image off in an SMS.
When I run the application locally, and use ngrok to expose my local machine to a publicly accessible URL (such as in e0f5c759.ngrok.io/reports/94109_17_03_21_14.jpeg), the application works fine. However, when I run my application on my AWS Lightsail EC2 Instance, and try to fetch the images from there (such as with 52.23.50.128:8080/reports/94109_17_03_21_14.jpeg or osterbox.com/reports/94109_17_03_21_14.jpeg), the SMS is never sent.
When I look at twilios debugger, I can see that the SMS never sends due to an HTTP connection failure. Specifically, in the response header I see:
Date Tue, 21 Mar 2017 21:44:01 GMT
Last-Modified Tue, 21 Mar 2017 21:44:00 GMT
Content-Type image/jpeg
Accept-Ranges bytes
Content-Length 77695
Server Jetty(9.3.3.v20150827)
X-Cache MISS from ip-172-18-7-38.ec2.internal
X-Cache-Lookup MISS from ip-172-18-7-38.ec2.internal:3128
Does anyone know why the HTTP Get request would fail?

Related

Spark java Serving mp3 file (dynamic with seek)

I'm trying to create a simple REST service to serve audio files (with seek support).
I'm using this example which is based on Jersey:
https://github.com/aruld/jersey-streaming/tree/jersey2
This is a quite simple example, it listens to GET and HEAD requests used by the browsers, look for the Range header and respond with 206 plus the archive slice requested (with byte ranges).
The catch here is that I'm re-writing this on spark java (a tiny framework with an embedded jetty server).
Every thing seems to be OK. The browser sends the GET and the server crates the response accordingly... though the player never loads nor plays anything. The request is made and the response header is perfect:
Request:
Host: localhost:4567
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Range: bytes=0-
Connection: keep-alive
Response:
Accept-Ranges: bytes
Content-Length: 1048577
Content-Range: bytes 0-1048576/5563904
Content-Type: audio/mp3
Date: Sat, 20 Aug 2016 05:41:23 GMT
Last-Modified: Sat Aug 20 01:12:37 BRT 2016
Server: Jetty(9.3.6.v20151106)
sasd
One thing I noticed is that seems the transfer for this request never ends. When I close the server, the transfer ends at 0,03KB (always).
My proof of concept app code:
http://pastebin.com/xjkLne7E
Found an answer!
I did some more research and found that it is a Spark feature to be implemented:
https://github.com/perwendel/spark/issues/397
User tchoulihan already tried to implement such feature with success here:
https://github.com/tchoulihan/torrenttunes-client
Here is a sample of the spark get request that handles the upload:
https://github.com/tchoulihan/torrenttunes-client/blob/master/src/main/java/com/torrenttunes/client/webservice/Platform.java#L555
I can't paste a blob here since GPLv3 would clash with cc-wiki license. Hes work is inspired on the same resource I first found. Based on that I have coded a version of my own that works on android, mozilla and chrome.
TL;DR The problem was that mozilla doesn't understand 206 request properly and I wasn't closing and flushing the ByteOutputStream. Also I tried to use the StreamingOutput from JAX as a response instead of sending a simple raw http response.

Wildfly JSF resources: err_invalid_chunked_encoding

My customer web application is currently on an AWS-EC2 Linux instance, running WildFly 8.2.0.Final "Tweek".
I'm not using Apache or other web servers, just accessing the application server on port 8080 (the system is forwarding to 8080 all traffic on port 80).
Application works fine everywhere, but yesterday I found a very weird issue on one of my customer's browsers. However, it's not related to the browser, because I already tried installing Firefox and Chrome, in addition to IE, and the problem is still there. The client OS is Win8.1.
The CSS and JS files are not loaded by the browser: Chrome prints this message in the console:
ERR_INVALID_CHUNKED_ENCODING
However, if I click on the file and 'Open link in new tab', I can actually see the file's content!
My static files are JSF resources, for example:
/javax.faces.resource/primefaces.css.htm?ln=primefaces&v=5.1
Response headers are, for example:
Content-Type:application/javascript
Date:Fri, 27 Mar 2015 16:32:51 GMT
ETag:W/"312861-1427473269000"
Expires:Fri, 03 Apr 2015 16:32:51 GMT
Last-Modified:Fri, 27 Mar 2015 16:21:09 GMT
I saw the content-length is also zero, which I suppose is the cause for the problem.
How to find out more info on that? What's the solution for that type of error? Thanks

Axis2 - Information Leakage Prevention

What is the easiest way to either configure Axis2 or extend the message listener to PREVENT any and all information regarding system from returning to the calling client?
An example of what I'm trying to prevent is as follows: Someone sends an improper soap request with some weird stuff in the header and the server responds:
HTTP/1.1 500 Internal Server Error
Date: Wed, 19 Nov 2014 13:12:34 GMT
Server: Apache
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)
Connection: close
Content-Length: 465
Content-Type: text/xml;charset=utf-8
...
<faultstring>javax.xml.stream.XMLStreamException: DOCTYPE is not allowed</faultstring>
What is the best way to prevent all of that information from being delivered back to the client? The glassfish messages can be turned off in GlassFish as answered below. I should have been more specific I want to set it up so that any and all exceptions never reach the client. I want to somehow force axis2 to use a generic message instead of returning an Exception. Is it possible to do this with Axis2?
You can add a
-Dproduct.name="".
in your JVM Option for suppressing the X-Powered-By

How does web page redirect work in this page?

I'm trying to retrieve links from this page: http://www.seas.harvard.edu/academics/areas
There is a link named "Computer Science" in the middle of the page. Its underlying link is given as "/academics/areas/computer-science". I'm able to convert it to an absolute URL with the Java built-in URL class, obtaining "http://www.seas.harvard.edu/academics/areas/computer-science".
When I click the link in Chrome browser, however, the absolute URL changes to "http://www.seas.harvard.edu/computer-science".
So my question is two-fold:
How does the URL redirect work in this page?
Is there any library or method in Java that would help me obtain the URL after redirect?
I need to obtain the URL after redirect because I want to read the source code of the page but the URL before redirect doesn't work for me. I'm using the JSoup library to read from the URL so I suspect it might be a javascript-based redirect.
From curl --dump-header [file] [URL] the file looked like:
HTTP/1.1 301 Moved Permanently
Age: 0
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: text/html
Date: Tue, 13 Aug 2013 13:00:12 GMT
ETag: "1376398812"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Tue, 13 Aug 2013 13:00:12 GMT
Location: http://www.seas.harvard.edu/computer-science
Server: nginx
Vary: Accept-Encoding
Via: 1.1 varnish
X-AH-Environment: prod
X-Cache: MISS
X-Drupal-Cache: MISS
X-Redirect-ID: 44
X-Varnish: 2704315535
transfer-encoding: chunked
Connection: keep-alive
As you can see this is a 301 permanent redirect served from the server.
To obtain the data:
You can use HttpURLConnection to connect, but before connecting, call myConn.setInstanceFollowRedirects(true). Redirects are followed and you can get your output stream and read it.
To obtain the URL itself:
You can use HttpURLConnection to connect, but before connecting, call myConn.setInstanceFollowRedirects(false) to not follow redirects. This will save the actual URL in the right place.
The trick here is that for some odd reason, HttpURLConnection doesn't allow to retrieve a header by name unless you parse it as a date.
So, you will need to iterate an integer, calling getHeaderFieldKey after making the connection and checking if it equal to Location and if it is, getting getHeaderField with the same integer to get the location. Annoying, I know. But a location isn't a date and this is a JRE oversight.
I used Fiddler to investigate and the site return for link http://www.seas.harvard.edu/academics/areas/computer-science HTTP 301 response code, that performs the redirect.
I you want to get real URL. You should perform real request to harvard.edu web server and parse response. (Redirect URL is located in Location key in HTTP Header).
Sorry about your second question. I don't have skill in Java.
This SO question may help (httpclient-4-how-to-capture-last-redirect-url)
There is probably e.g. a .htaccess and mod_rewrite redirect. Using Firefox's Console I could see the requests. As you can see below the server is sending back a 301 Moved Permanently message. This tells the browser to redirect to the address returned in the Location header of the response.
The way you obtain the changed URL depends on the way you load the page:
If you use ready libraries & code to load the page to e.g. a DOM object, the you could use that ready HTTP system to load the response, this will probably result to it automatically redirecting -> you will get the URL from the URL of the loaded page. If it does not do that, then you must check for status code 301 or 302 and when those are received then the changed URL is in the Location header of the response.
If you have your own code written to load the response via TCP sockets, then you must just load the response as normal, but again check for the 301 and 302 status codes and do as described in the previous section.
I can only attempt to address Q1 since I'm not a Java programmer. The source code says they're using Drupal, so I speculate that they're using Drupal's global redirect module (SO discussion about Drupal redirect module here). Looking at the module's documentation might shed some light on how to obtain the correct url with Java.
There's also numerous ways within javascript to have url requests automatically redirect to some base page (e.g., CS homepage), while physically navigating the site allows the user to advance to new pages. This is standard practice in many single page web apps. If this is the case, then #hexafraction 's suggestion might be able to help you retrieve the desired url, though I'm unfamiliar with the Java methods (s)he is suggesting.
You can get the Redirect URL from the below code setting followRedirects to false.
You will get the source code of the redirected page if you set it to true and that's the default behavior of Jsoup
Connection con = Jsoup.connect("http://www.seas.harvard.edu/academics/areas/computer-science")
.userAgent("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36")
.followRedirects(false);
System.out.println("Redirected Url : " + con.execute().header("Location")); //null if followRedirect is true
Document doc = con.get();
System.out.println(doc.html());
System.out.println("=================================================");

Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java

I am trying to upload image/file to google cloud storage from my GAE application using new Gcs Client Library.
Here is the code snippet
GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);
GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/html").acl("public-read").build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename,options);
PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
out.println("The woods are lovely dark and deep.");
out.println("But I have promises to keep.");
out.flush();
writeChannel.waitForOutstandingWrites();
writeChannel.write(ByteBuffer.wrap("And miles to go before I sleep.".getBytes()));
writeChannel.close();
When i look into the logs i am getting 403 error like this
Server replied with 403, check that ACLs are set correctly on the object and bucket:
Request: POST https://storage.googleapis.com/<bucket name>/<object name>
x-goog-resumable: start
x-goog-api-version: 2
Content-Type: text/html
x-goog-acl: public-read
no content
Response: 403 with 152 bytes of content
Content-Type: application/xml; charset=UTF-8
Content-Length: 152
Date: Tue, 02 Jul 2013 14:10:02 GMT
Server: HTTP Upload Server Built on Jun 28 2013 13:27:54 (1372451274)
X-Google-Cache-Control: remote-fetch
Via: HTTP/1.1 GWA
<?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>images2.solestruck.com</Details></Error>
Can someone help me in fixing this.
I have the same problem. Follow instructions below (https://developers.google.com/appengine/docs/java/googlestorage/#Java_Prerequisites)
Give permissions to your bucket or objects. To enable your app to
create new objects in a bucket, you need to do the following:
Log into the App Engine Admin Console. Click on the application you
want to authorize for your Cloud Storage bucket. Click on Application
Settings under the Administration section on the left-hand side. Copy
the value under Service Account Name. This is the service account name
of your application, in the format
application-id#appspot.gserviceaccount.com. If you are using an App
Engine Premier Account, the service account name for your application
is in the format
application-id.example.com#appspot.gserviceaccount.com.
Grant access permissions using the following methods: The easiest way
to grant app access to a bucket is to use the Google APIs Console to
add the service account name of the app as a team member to the
project that contains the bucket.(The app should have edit permissions
if it needs to write to the bucket.) For information about permissions
in Cloud Storage, see Scopes and Permissions. Add more apps to the
project team if desired.
It works for me.

Categories

Resources