Skip to content

Commit

Permalink
Fix for JERSEY-1268.
Browse files Browse the repository at this point in the history
Change-Id: I47a5053287683b13b8edb1f6b3bb50bd7a57f08f
  • Loading branch information
japod committed Sep 3, 2012
1 parent c0d4575 commit f6cafea
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ nbactions.xml
.settings/*
.project
.classpath
examples/helloworld-pure-jax-rs/nb-configuration.xml
examples/helloworld/nb-configuration.xml
examples/osgi-http-service/bundle/nb-configuration.xml
examples/osgi-http-service/functional-test/nb-configuration.xml
tests/e2e/nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.ServiceUnavailableException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.ClientException;
import javax.ws.rs.client.Entity;
Expand Down Expand Up @@ -690,8 +701,51 @@ public void failed(ClientException error) {

private ClientException convertToException(Response response) {
try {
// TODO proper exception selection
return new ClientException(new WebApplicationException(response));
WebApplicationException webAppException = null;
final int statusCode = response.getStatus();
switch (Response.Status.fromStatusCode(statusCode)) {
case BAD_REQUEST:
webAppException = new BadRequestException(response);
break;
case UNAUTHORIZED:
webAppException = new NotAuthorizedException(response);
break;
case NOT_FOUND:
webAppException = new NotFoundException(response);
break;
case METHOD_NOT_ALLOWED:
webAppException = new NotAllowedException(response);
break;
case NOT_ACCEPTABLE:
webAppException = new NotAcceptableException(response);
break;
case UNSUPPORTED_MEDIA_TYPE:
webAppException = new NotSupportedException(response);
break;
case MOVED_PERMANENTLY:
case FOUND:
case SEE_OTHER:
case TEMPORARY_REDIRECT:
webAppException = new RedirectionException(response);
break;
case INTERNAL_SERVER_ERROR:
webAppException = new InternalServerErrorException(response);
break;
case SERVICE_UNAVAILABLE:
webAppException = new ServiceUnavailableException(response);
break;
default:
if (299 < statusCode && statusCode < 400) {
webAppException = new RedirectionException(response);
} else if (399 < statusCode && statusCode < 500) {
webAppException = new ClientErrorException(response);
} else if (499 < statusCode && statusCode < 600) {
webAppException = new ServerErrorException(response);
} else {
webAppException = new WebApplicationException(response);
}
}
return new ClientException(webAppException);
} catch (Throwable t) {
return new ClientException(LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
}
Expand Down
4 changes: 1 addition & 3 deletions etc/jersey-1-migrated-tests
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ media/multipart/src/test/java/org/glassfish/jersey/media/multipart/internal/Mult
media/multipart/src/test/java/org/glassfish/jersey/media/multipart/MultipartMediaTypesTest.java
media/multipart/src/test/java/org/glassfish/jersey/media/multipart/MultiPartTest.java
media/multipart/src/test/java/org/glassfish/jersey/media/multipart/ParameterizedHeadersMapTest.java
tests/e2e/src/test/java/org/glassfish/jersey/tests/api/ExceptionTest.java
tests/e2e/src/test/java/org/glassfish/jersey/tests/api/VariantsTest.java
tests/e2e/src/test/java/org/glassfish/jersey/tests/api/ResourceContextTest.java
tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/GenericTypeAndEntityTest.java
Expand Down Expand Up @@ -347,7 +348,6 @@ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/ResourceFilterFa
jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/UriModificationFilterTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/CanonicalizationFeatureTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/EscapedURITest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/ExceptionTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/GZIPContentEncodingTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/HeadTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/HttpMethodTest.java
Expand All @@ -361,7 +361,6 @@ jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/Application
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/ApplicationTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/CanonicalizationFeatureTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/EscapedURITest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/ExceptionTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/FixedRequestSizeTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/GrizzlyWebProviderLifecycleTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/GZIPContentEncodingTest.java
Expand All @@ -385,7 +384,6 @@ jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/AcceptableXM
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/ApplicationConfigTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/CanonicalizationFeatureTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/EscapedURITest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/ExceptionTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/GZIPContentEncodingTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/HandlerContextTest.java
jersey-tests/src/test/java/com/sun/jersey/impl/container/httpserver/HeadTest.java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/

package org.glassfish.jersey.tests.api;


import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.GET;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.ServiceUnavailableException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* Test for WebApplicationException handling on both server and client side.
*
* @author Paul Sandoz (paul.sandoz at oracle.com)
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
*/
public class ExceptionTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(ExceptionDrivenResource.class, ResponseDrivenResource.class);
}

@Override
protected void configureClient(ClientConfig clientConfig) {
clientConfig.setProperty(ClientProperties.FOLLOW_REDIRECTS, false);
}

static final URI testUri = UriBuilder.fromUri("http://jersey.java.net").build();

static Map<String, WebApplicationException> ExceptionMAP = new HashMap<String, WebApplicationException>() {{
put("301", new RedirectionException(Response.Status.MOVED_PERMANENTLY, testUri));
put("302", new RedirectionException(Response.Status.FOUND, testUri));
put("303", new RedirectionException(Response.Status.SEE_OTHER, testUri));
put("307", new RedirectionException(Response.Status.TEMPORARY_REDIRECT, testUri));
put("400", new BadRequestException());
put("401", new NotAuthorizedException("challenge"));
put("402", new ClientErrorException(402));
put("404", new NotFoundException());
put("405", new NotAllowedException("OPTIONS"));
put("406", new NotAcceptableException());
put("415", new NotSupportedException());
put("500", new InternalServerErrorException());
put("501", new ServerErrorException(501));
put("503", new ServiceUnavailableException());
}};

static Map<String, Response> ResponseMAP = new HashMap<String, Response>() {{
put("301", Response.status(301).location(testUri).build());
put("302", Response.status(302).location(testUri).build());
put("303", Response.seeOther(testUri).build());
put("307", Response.temporaryRedirect(testUri).build());
put("400", Response.status(400).build());
put("401", Response.status(401).build());
put("402", Response.status(402).build());
put("404", Response.status(404).build());
put("405", Response.status(405).allow("OPTIONS").build());
put("406", Response.status(406).build());
put("415", Response.status(415).build());
put("500", Response.serverError().build());
put("501", Response.status(501).build());
put("503", Response.status(503).build());
}};

@Path("exceptionDriven")
public static class ExceptionDrivenResource {

@GET
@Path("{status}")
public String get(@PathParam("status") String status) {
throw ExceptionMAP.get(status);
}
}

@Path("responseDriven")
public static class ResponseDrivenResource {

@GET
@Path("{status}")
public Response get(@PathParam("status") String status) {
return ResponseMAP.get(status);
}
}

private void _testStatusCode(final String status) {
_testStatusCodeViaException("exceptionDriven", status);
_testStatusCodeDirectly("exceptionDriven", status);
_testStatusCodeViaException("responseDriven", status);
_testStatusCodeDirectly("responseDriven", status);
}

private void _testStatusCodeViaException(final String prefix, final String status) {

final int statusCode = Integer.parseInt(status);

try {

target().path(prefix).path(status).request().get(ClientResponse.class);
fail("An exception expected");
} catch (WebApplicationException ex) {
assertEquals(ExceptionMAP.get(status).getClass(), ex.getClass());

final Response response = ex.getResponse();
assertEquals(statusCode, response.getStatus());

if (is3xxCode(statusCode)) {
assertNotNull(response.getLocation());
}
}
}

private void _testStatusCodeDirectly(final String prefix, final String status) {
final int statusCode = Integer.parseInt(status);
final Response response = target().path(prefix).path(status).request().get();
assertEquals(statusCode, response.getStatus());
if (is3xxCode(statusCode)) {
assertNotNull(response.getLocation());
}
}


@Test
public void testAllStatusCodes() {
for (String status : ExceptionMAP.keySet()) {
_testStatusCode(status);
}
}

private boolean is3xxCode(final int statusCode) {
return 299 < statusCode && statusCode < 400;
}
}

0 comments on commit f6cafea

Please sign in to comment.