Handled 301 Moved Permanently responses

This commit is contained in:
Rohit Awate 2018-01-27 22:49:25 +05:30
parent f286c6853d
commit 850a74cd11

View file

@ -45,10 +45,24 @@ public class RequestManager {
if (serverResponse == null)
throw new IOException();
else if (serverResponse.getStatus() == 301) {
response.setStatusCode(301);
String newLocation = serverResponse.getHeaderString("location");
String responseHelpText;
if (newLocation == null)
responseHelpText = "The resource has been permanently moved to another location.\n\n" +
"Here's what you can do:\n" +
"- Find the new URL from the API documentation.\n" +
"- Try using https instead of http if you're not already.";
else
responseHelpText = "The resource has been permanently moved to: " + newLocation;
response.setBody(responseHelpText);
return response;
}
System.out.println(serverResponse);
String type = (String) serverResponse.getHeaders().getFirst("Content-type");
System.out.println(type);
String responseBody = serverResponse.readEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
@ -60,6 +74,7 @@ public class RequestManager {
JsonNode node = mapper.readTree(responseBody);
response.setBody(mapper.writeValueAsString(node));
break;
case "application/xml; charset=utf-8":
case "application/xml":
response.setBody(mapper.writeValueAsString(responseBody));
break;