下面列出了怎么用javax.ws.rs.RedirectionException的API类实例代码及写法,或者点击链接到github查看源代码。
private Response handleException(final Throwable err) {
final Throwable cause = err.getCause();
if (cause instanceof StorageConflictException) {
LOGGER.debug("Storage conflict error: {}", err.getMessage());
LOGGER.trace("Storage conflict error: ", err);
return Response.status(Response.Status.CONFLICT).build();
} else if (cause instanceof ClientErrorException) {
LOGGER.debug("Client error: {}", err.getMessage());
LOGGER.trace("Client error: ", err);
} else if (cause instanceof RedirectionException) {
LOGGER.debug("Redirection: {}", err.getMessage());
LOGGER.trace("Redirection: ", err);
} else {
LOGGER.error("Error:", err);
}
return cause instanceof WebApplicationException
? ((WebApplicationException) cause).getResponse()
: new WebApplicationException(err).getResponse();
}
/**
* Check for a conditional operation.
* @param method the HTTP method
* @param ifNoneMatch the If-None-Match header
* @param etag the resource etag
*/
public static void checkIfNoneMatch(final String method, final String ifNoneMatch, final EntityTag etag) {
if (ifNoneMatch == null) {
return;
}
final Set<String> items = stream(ifNoneMatch.split(",")).map(String::trim).collect(toSet());
if (isGetOrHead(method)) {
if ("*".equals(ifNoneMatch) || items.stream().map(EntityTag::valueOf)
.anyMatch(e -> e.equals(etag) || e.equals(new EntityTag(etag.getValue(), !etag.isWeak())))) {
throw new RedirectionException(notModified().build());
}
} else {
if ("*".equals(ifNoneMatch) || items.stream().map(EntityTag::valueOf).anyMatch(isEqual(etag))) {
throw new ClientErrorException(status(PRECONDITION_FAILED).build());
}
}
}
public static Class<?> getWebApplicationExceptionClass(Response exResponse,
Class<?> defaultExceptionType) {
int status = exResponse.getStatus();
Class<?> cls = EXCEPTIONS_MAP.get(status);
if (cls == null) {
int family = status / 100;
if (family == 3) {
cls = RedirectionException.class;
} else if (family == 4) {
cls = ClientErrorException.class;
} else if (family == 5) {
cls = ServerErrorException.class;
}
}
return cls == null ? defaultExceptionType : cls;
}
@Test( expected = RedirectionException.class )
public void downloadLatestVersion()
throws Exception
{
String id = createAndScanRepo();
try
{
Response response =
getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null,
null );
}
catch ( RedirectionException e )
{
Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
+ "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo(
0 );
throw e;
}
finally
{
getManagedRepositoriesService().deleteManagedRepository( id, false );
}
}
@GET
@Path("redirect/{status}")
public Response redirect(@PathParam("status") int status) {
if (status == 307) {
throw new RedirectionException(status, URI.create("hello"));
}
return Response.status(status).header("Location", "/hello").build();
}
/**
* Check for a conditional operation.
* @param method the HTTP method
* @param ifModifiedSince the If-Modified-Since header
* @param modified the resource modification date
*/
public static void checkIfModifiedSince(final String method, final String ifModifiedSince,
final Instant modified) {
if (isGetOrHead(method)) {
final Instant time = parseDate(ifModifiedSince);
if (time != null && time.isAfter(modified.truncatedTo(SECONDS))) {
throw new RedirectionException(notModified().build());
}
}
}
private void handleTrailingSlashRedirection(final Resource resource) {
if (getRequest().hasTrailingSlash() && !isContainer(resource.getInteractionModel())) {
throw new RedirectionException(303, create(getBaseUrl() + normalizePath(getRequest().getPath())));
} else if (!getRequest().hasTrailingSlash() && !getRequest().getPath().isEmpty()
&& isContainer(resource.getInteractionModel())) {
throw new RedirectionException(303, create(getBaseUrl() + normalizePath(getRequest().getPath()) + "/"));
}
}
@Test
void testCheckIfModifiedSince() {
final String time = "Wed, 21 Oct 2015 07:28:00 GMT";
final Instant timestamp1 = ofEpochSecond(1445412479);
assertThrows(RedirectionException.class, () -> HttpUtils.checkIfModifiedSince("GET", time, timestamp1));
final Instant timestamp2 = ofEpochSecond(1445412480);
assertDoesNotThrow(() -> HttpUtils.checkIfModifiedSince("GET", time, timestamp2));
}
@Test( expected = RedirectionException.class )
public void downloadFixedVersion()
throws Exception
{
String id = createAndScanRepo();
try
{
Response response =
getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null,
null );
}
catch ( RedirectionException e )
{
Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id
+ "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo(
0 );
throw e;
}
finally
{
getManagedRepositoriesService().deleteManagedRepository( id, false );
}
}