2017-12-15 00:01:26 +01:00
|
|
|
package eu.eudat.proxy;
|
2017-09-29 14:13:21 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
public class Proxy {
|
|
|
|
|
|
|
|
private String allowedHost;
|
|
|
|
|
|
|
|
public Proxy(String allowedHost) throws MalformedURLException {
|
|
|
|
this.allowedHost = new URL(allowedHost).getHost();
|
|
|
|
}
|
|
|
|
|
2017-12-15 00:01:26 +01:00
|
|
|
public Proxy(){
|
|
|
|
|
|
|
|
}
|
2017-09-29 14:13:21 +02:00
|
|
|
|
2017-12-15 00:01:26 +01:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/eu/eudat/proxy"}, produces="application/json")
|
2017-09-29 14:13:21 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> proxy(@RequestParam("url") String remoteUrl) {
|
|
|
|
|
|
|
|
StringBuffer response = new StringBuffer();
|
|
|
|
URL url;
|
|
|
|
try {
|
|
|
|
URL tempUrl = new URL(remoteUrl);
|
|
|
|
// URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);
|
|
|
|
URI uri = new URI(tempUrl.getProtocol(), null, tempUrl.getHost(), tempUrl.getPort(), tempUrl.getPath(), (tempUrl.getQuery()!=null)?URLEncoder.encode(tempUrl.getQuery()):null, tempUrl.getRef());
|
|
|
|
url = uri.toURL();
|
|
|
|
|
|
|
|
if(!url.getHost().equals(allowedHost))
|
2017-12-15 00:01:26 +01:00
|
|
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("{'reason': 'You are not allowed to eu.eudat.proxy -> "+url.getHost()+"'}");
|
2017-09-29 14:13:21 +02:00
|
|
|
//if allowed, proceed
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.setRequestProperty("Accept", "application/vnd.api+json; charset=utf-8");
|
|
|
|
|
|
|
|
int responseCode = con.getResponseCode();
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) { // success
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
|
String inputLine;
|
|
|
|
while ((inputLine = in.readLine()) != null)
|
|
|
|
response.append(inputLine);
|
|
|
|
in.close();
|
2017-10-18 15:12:25 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(response.toString());
|
|
|
|
}
|
|
|
|
else {
|
2017-09-29 14:13:21 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("{'reason': 'Remote server responded with: "+responseCode+"'}");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException | URISyntaxException e) {
|
2017-12-15 00:01:26 +01:00
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{'reason': 'Could not eu.eudat.proxy to given host'}");
|
2017-09-29 14:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|