Image getting 403 forbidden when deployed to server - java

Background:
I am using Spring web MVC along with JSP and HTML/JavaScript to write a website. I have added a search function, which sends a query to imdbapi.org and receives a JSON object containing movie/tv show information (via AJAX). The JSON object contains a "poster" field for each result, which is a URL to an image (on imdb server). The results are then displayed showing the poster if available, using jQuery..
someDiv.append($("<img src='"+results[i].poster+"'></img'").addClass("resultImg"));
Problem:
This works absolutely fine when running on STS built-in server and accessing locally eg.
http://localhost:8080/myWebPage.whatever
I have a ubuntu server box with Tomcat 7 installed, when deploying to the server I get a 403 error for each image. Example from chrome console:
GET http://ia.media-imdb.com/images/M/MV5BMTY2NDY4NDA0OV5BMl5BanBnXkFtZTcwNTI4MzUyMQ##._V1._SY317_CR5,0,214,317_.jpg 403 (Forbidden)
More Info:
Have tested in chrome and firefox with same results.
If I cannot fix then one solution would be to download to a temp folder on the server I think...
Is this a configuration problem on my box with the tomcat server?

Is this a configuration problem on my box with the tomcat server?
No, Not at all, Your server configuration has nothing to do with the external resource accessibility,If you are getting #._V1._SY317_CR5,0,214,317_.jpg 403 (Forbidden) - this clearly mean's that the server is refusing your request.
And it seem's many people's have these kind of problem with IMDB - see this

I have "made it work", it is not a direct solution to the question but a workaround. Using a local proxy on my server I download the image into memory in java and then return to the web page...
Java (server side)
#RequestMapping(value="/pages/proxyImg")
public ResponseEntity<byte[]> proxyImage(String url) {
log.info("Image Proxy server: " + url);
try {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(new URL(url).openConnection().getInputStream()), headers, HttpStatus.CREATED);
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
Javascript
someDiv.append($("<img src='"+"proxyImg.htm?url="+results[i].poster+"'></img'"));

Related

ImageIO.read( new URL(imagePath)) is not responding for HTTPS URLS

My code is working as expected on localhost starter kit but when I deployed it to development server my below code is taking around 2-3 mins and after that it says
Invalid URL
The requested URL "http://%5bNo%20Host%5d/index.html?", is invalid.
Reference #9.3d7c4117.1550338465.3d54af04
My code is as follows:
imagePath = "https://i.imgur.com/TrNzuMY.jpg";
Image image = null;
image = ImageIO.read( new URL(imagePath))
Is this something AEM on development server is not allowing to access external URLs?
This is AEM 6.3 and this code is called in a JSP which is getting called by another JSP using AJAX GET request.
Edit (After Sumanta Pakira response): This is only happening when passed URL is Secure i.e. HTTPS, for HTTP URLs it is working as expected.
There are two solutions :
Add the server (i.imgur.com) certificate into your AEM server trust store.
You can look at this example

Web-service reference in NetBeans project

I created .NET console WS-host. Now I'd like to create Java WS-client in NetBeans 8.0.
But I've faced to a problem. NetBeans returns an error in window: "Problem with downloading wsdl or schema file. Check the URL, proxy settings or whether the server is running. URL: http://localhost:8080/?wsdl."©
Also output: "Error: An I/O error occured. Server returned HTTP response code: 502 for URL: http://localhost:8080/?wsdl"©
I can open the URL in a browser. It's fine. Also .NET web-client works fine with the host.
Accidentally, I've used 127.0.0.1 instead of localhost in Chrome and IE. IE works fine and displays WSDL, but Chrome returns "Error Code: 502 Proxy Error."©
Where could be a problem?
So, the problem was resolved by changing of NetBeans Proxy Settings. I set it to No Proxy instead of Use System Proxy Settings.

'Access-Control-Allow-Origin' with Socket.IO on Java Server

I've got the following problem: I'm running a JAVA Server with socket.io (netty socket.io - https://github.com/mrniko/netty-socketio) - I'm trying to access this server from a different web-server through javascript.
For a test I'm trying to get the Demo Chat running (https://github.com/mrniko/netty-socketio-demo).
The Problem now is that i keep getting the following
XMLHttpRequest cannot load http://myserver/socket.io/1/?t=1400445162388. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'myclient.com' is therefore not allowed access.
So the big question is: How do i allow access to my java server? I've found a few solutions saying to add the 'header' to it, but i have no idea where to put that. Does that go into the socket.io server code?
I think i need something like this, placed somewhere on the JAVA server:
response.setHeader("Access-Control-Allow-Origin", "*");
This is the code which starts the server:
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(80);
final SocketIOServer server = new SocketIOServer(config);
//ChatObject wurde extra implementiert, ggf. loeschen
server.addJsonObjectListener(ChatObject.class, new DataListener<ChatObject>() {
#Override
public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) throws Exception {
// broadcast messages to all clients
server.getBroadcastOperations().sendJsonObject(data);
}
});
server.start();
and this is the output im getting from the server:
error: The specified resource was not found: /static/flashsocket/WebSocketMain.swf
error: The specified resource was not found: /static/flashsocket/WebSocketMainInsecure.swf
info: Session store / pubsub factory used: {}MemoryStoreFactory (local session store only)
info: SocketIO server started at port: {}80
Any help is greatly appreciated ! Thank you
I've confronted the similar problem yesterday and solved it mostly. I'd like to share my solution here.
Firstly, we all know it is so called CORS standard (cross-origin resource sharing) which means one resource we opened up from website A request another resource from website B (reference). Therefore, in your case, assume the original web server (with domain AAA.com) and your netty server (domain BBB.com), then append this line to your netty server to get permission:
config.setOrigin("http://AAA.com");
Note that you cannot use wildcard (*) for Access-Control-Allow-Origin in netty-socketio as it set credentials (i.e., Access-Control-Allow-Credentials) TRUE by default (see here). Keep in mind that the browser will also reject any response that does not have the Access-Control-Allow-Credentials: true header, and not make the response available to the invoking web content. Hope these help.
The followings are my library versions:
netty-socketio version
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.6</version>
</dependency>
socket.io.javascript
script(type="text/javascript", src="https://cdn.socket.io/socket.io-1.3.5.js")
Use netty-socketio 1.7.1 or 1.6.6 version. Ability to set custom Access-Control-Allow-Origin via Configuration.origin parameter was added in this version.

How to get a response from localhost server in GWT client side?

I have a WCF based web service and hosted on local machine written in c#. I want to get a response from this hosted service in my GWT client side.
Please check the following code:
url = "localhost:8089/request"
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
Request response = builder.sendRequest(null, new RequestCallback() {
#override
public void onResponseReceived(Request request, Response response) {
Window.alert(response + " ");
}
});
Everytime when i am trying to run the code it shows an "http://localhost:8089 is not allowed by Access-Control-Allow-Origin."
PS: i searched for this error but i don't want to disable the web security of all browsers.
Is there any alternative solution to do get response of localhost server running on different porn on same machine. That is why i want to call a url in client side.
Please suggest a solution.
Edit
Let me explain you full scenario in points:
GWT application is hosted on some server (www.abc.com)
WCF is a web service installed at each client.
A client open his/her browser and put the url (www.abc.com/page)
This page want to access the web services hosted on a client machine.
Can't you configure your WCF service to send an Access-Control-Allow-Origin: www.abc.com response header?
Otherwise, I guess you could use a proxy servlet in www.abc.com that proxies the call to the getRemoteAddr, assuming there's no proxy in-between.

How do I open a website in browser through JAVA without having the browser open a new tab

Introduction
I am making a proxy server in java. Whenever the user enters an unknown host (URL) in the browser, the proxy server handles UnknownHostException by executing the code below:
try {
Process p=Runtime.getRuntime().exec("cmd /c start http://www.mysite.com/unknownhosterror.htm");
}
catch(IOException io) {
System.out.println("Error");
}
What these lines of code do is to display an html file containing "This page could
not be displayed." whenever the user entered a non-existing URL.
Problem
The code above opens a new tab and displays the content of www.mysite.com/unknownhosterror.htm. What I want is to redirect to it.
For example, I wrote www.nosuchsite.com in the URL bar. Suppose there is no such site, it will automatically redirect to www.mysite.com/unknownhosterror.htm and display "This page could not be displayed.".
How can I do this?
EDIT NOTE: I do not use Servlet.
I may be misunderstanding what you mean but if you indeed have a proxy server, they you should be able to issue a 301 redirect back to the browser when the proxy server detects the UnknownHostException.
In the response to the browser, you need to add something like the following lines to the header of your response:
HTTP/1.1 301 Moved Permanently
Location: http://www.mysite.com/unknownhosterror.htm
How to add that to your headers depends highly on how you are handling the requests. If you show a little bit of your proxy handler code, I can provide more information.

Categories

Resources