下面列出了org.apache.http.HttpStatus#SC_SEE_OTHER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (!enableRedirects) {
return false;
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
} //end of switch
}
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context)
throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
final int statusCode = response.getStatusLine().getStatusCode();
final String method = request.getRequestLine().getMethod();
final Header locationHeader = response.getFirstHeader("location");
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
return isRedirectable(method) && locationHeader != null;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
case 308: // Ensure that HTTP 308 is redirected as well
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} // end of switch
}
/**
* Engineとして許容しないレスポンスコードかどうかを判定する.
* @param oStatus レスポンスコード(Number型)
* @return true:Engineとして許容しないレスポンスコードである false:Engineとして許容できるレスポンスコードである
*/
private static boolean isInvalidResCode(Number oStatus) {
// 以下のレスポンスコードは許容しない
// ・3桁ではない
// ・0番台
// ・100番台(クライアントの挙動が不安定になるため)
if (!String.valueOf(Context.toString(oStatus)).matches("^[2-9]\\d{2}$")) {
return true;
}
// 301、303、307はサーブレットコンテナでエラーになるため許容しない
if (oStatus.intValue() == HttpStatus.SC_MOVED_PERMANENTLY
|| oStatus.intValue() == HttpStatus.SC_SEE_OTHER
|| oStatus.intValue() == HttpStatus.SC_TEMPORARY_REDIRECT) {
return true;
}
return false;
}
/**
* Check if status is a redirect (various 30x values).
*
* @param status Http status
* @return true if status is a redirect
*/
public static boolean isRedirect(int status) {
return status == HttpStatus.SC_MOVED_PERMANENTLY
|| status == HttpStatus.SC_MOVED_TEMPORARILY
|| status == HttpStatus.SC_SEE_OTHER
|| status == HttpStatus.SC_TEMPORARY_REDIRECT;
}
/**
* Updates all URLS to a given URN.
* <br><br>
* 204 No Content: URN was updated successfully<br>
* 301 Moved Permanently: URN has a newer version<br>
* 303 See other: URL is registered for another URN<br>
*
* @return the status code of the request
*/
private Optional<Date> update(MCRPIRegistrationInfo urn) {
MCREpicurLite elp = epicurProvider.apply(urn);
String elpXML = elp.asXMLString();
String updateURL = getUpdateURL(urn);
CloseableHttpResponse response = MCRHttpsClient.post(updateURL, APPLICATION_XML.toString(), elpXML);
StatusLine statusLine = response.getStatusLine();
if (statusLine == null) {
LOGGER.warn("POST request for {} returns no status line.", updateURL);
return Optional.empty();
}
int postStatus = statusLine.getStatusCode();
String identifier = urn.getIdentifier();
switch (postStatus) {
case HttpStatus.SC_NO_CONTENT:
LOGGER.info("URN {} updated to {}.", identifier, elp.getUrl());
return Optional.ofNullable(response.getFirstHeader("Last-Modified"))
.map(Header::getValue)
.map(DateTimeFormatter.RFC_1123_DATE_TIME::parse)
.map(Instant::from)
.map(Date::from);
case HttpStatus.SC_MOVED_PERMANENTLY:
LOGGER.warn("URN {} has a newer version.", identifier);
break;
case HttpStatus.SC_SEE_OTHER:
LOGGER.warn("URL {} is registered for another URN.", elp.getUrl());
break;
default:
LOGGER.warn("URN {} could not be updated. Status {}.", identifier, postStatus);
break;
}
return Optional.empty();
}
private static boolean isRedirectStatus(int statusCode) {
switch (statusCode) {
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return true;
default:
return false;
}
}
@Override
public boolean isRedirected(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
String method = request.getRequestLine().getMethod();
Header locationHeader = response.getFirstHeader("location");
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
return isRedirectable(method) && locationHeader != null;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
/**
* Registers a new URN.
* <br><br>
* 201 Created: URN-Record is successfully created.<br>
* 303 See other: At least one of the given URLs is already registered under another URN,
* which means you should use this existing URN instead of assigning a new one<br>
* 409 Conflict: URN-Record already exists and can not be created again.<br>
*
* @return the status code of the request
*/
private Optional<Date> registerNew(MCRPIRegistrationInfo urn) {
MCREpicurLite elp = epicurProvider.apply(urn);
String elpXML = elp.asXMLString();
String baseServiceURL = getBaseServiceURL(urn);
CloseableHttpResponse response = MCRHttpsClient.put(baseServiceURL, APPLICATION_XML.toString(), elpXML);
StatusLine statusLine = response.getStatusLine();
if (statusLine == null) {
LOGGER.warn("PUT request for {} returns no status line.", baseServiceURL);
return Optional.empty();
}
int putStatus = statusLine.getStatusCode();
String identifier = urn.getIdentifier();
URL url = elp.getUrl();
switch (putStatus) {
case HttpStatus.SC_CREATED:
LOGGER.info("URN {} registered to {}", identifier, url);
return Optional.ofNullable(response.getFirstHeader("Last-Modified"))
.map(Header::getValue)
.map(DateTimeFormatter.RFC_1123_DATE_TIME::parse)
.map(Instant::from)
.map(Date::from);
case HttpStatus.SC_SEE_OTHER:
LOGGER.warn("At least one of the given URLs is already registered under another URN, "
+ "which means you should use this existing URN instead of assigning a new one.");
LOGGER.warn("URN {} could NOT registered to {}.", identifier, url);
break;
case HttpStatus.SC_CONFLICT:
LOGGER.warn("URN-Record already exists and can not be created again.");
LOGGER.warn("URN {} could NOT registered to {}.", identifier, url);
break;
default:
LOGGER.warn("Could not handle urnInfo request: status={}, urn={}, url={}.", putStatus, identifier, url);
LOGGER.warn("Epicur Lite:");
LOGGER.warn(elpXML);
break;
}
return Optional.empty();
}
public static FetchStatus mapHttpStatusToFetchStatus(int httpStatus) {
switch (httpStatus) {
case HttpStatus.SC_OK:
return FetchStatus.FETCHED;
case HttpStatus.SC_FORBIDDEN:
return FetchStatus.HTTP_FORBIDDEN;
case HttpStatus.SC_UNAUTHORIZED:
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
return FetchStatus.HTTP_UNAUTHORIZED;
case HttpStatus.SC_NOT_FOUND:
return FetchStatus.HTTP_NOT_FOUND;
case HttpStatus.SC_GONE:
return FetchStatus.HTTP_GONE;
case HttpStatus.SC_TEMPORARY_REDIRECT:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
return FetchStatus.HTTP_TOO_MANY_REDIRECTS;
case HttpStatus.SC_MOVED_PERMANENTLY:
return FetchStatus.HTTP_MOVED_PERMANENTLY;
default:
if (httpStatus < 300) {
LOGGER.warn("Invalid HTTP status for exception: " + httpStatus);
return FetchStatus.HTTP_SERVER_ERROR;
} else if (httpStatus < 400) {
return FetchStatus.HTTP_REDIRECTION_ERROR;
} else if (httpStatus < 500) {
return FetchStatus.HTTP_CLIENT_ERROR;
} else if (httpStatus < 600) {
return FetchStatus.HTTP_SERVER_ERROR;
} else {
LOGGER.warn("Unknown status: " + httpStatus);
return FetchStatus.HTTP_SERVER_ERROR;
}
}
}
private int computeStatus(int status) {
switch (status) {
case HttpStatus.SC_FORBIDDEN:
case HttpStatus.SC_METHOD_NOT_ALLOWED:
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_UNAUTHORIZED:
case HttpStatus.SC_PAYMENT_REQUIRED:
case HttpStatus.SC_NOT_FOUND:
case HttpStatus.SC_NOT_ACCEPTABLE:
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
case HttpStatus.SC_REQUEST_TIMEOUT:
case HttpStatus.SC_CONFLICT:
case HttpStatus.SC_GONE:
case HttpStatus.SC_LENGTH_REQUIRED:
case HttpStatus.SC_PRECONDITION_FAILED:
case HttpStatus.SC_REQUEST_TOO_LONG:
case HttpStatus.SC_REQUEST_URI_TOO_LONG:
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
case HttpStatus.SC_EXPECTATION_FAILED:
case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
case HttpStatus.SC_METHOD_FAILURE:
case HttpStatus.SC_UNPROCESSABLE_ENTITY:
case HttpStatus.SC_LOCKED:
case HttpStatus.SC_FAILED_DEPENDENCY:
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_NOT_IMPLEMENTED:
case HttpStatus.SC_BAD_GATEWAY:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case HttpStatus.SC_GATEWAY_TIMEOUT:
case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
case HttpStatus.SC_INSUFFICIENT_STORAGE:
return 0;
case HttpStatus.SC_CONTINUE:
case HttpStatus.SC_SWITCHING_PROTOCOLS:
case HttpStatus.SC_PROCESSING:
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
case HttpStatus.SC_NO_CONTENT:
case HttpStatus.SC_RESET_CONTENT:
case HttpStatus.SC_PARTIAL_CONTENT:
case HttpStatus.SC_MULTI_STATUS:
case HttpStatus.SC_MULTIPLE_CHOICES:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_NOT_MODIFIED:
case HttpStatus.SC_USE_PROXY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return 1;
default :
return 1;
}
}