|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.tests.e2e.client; |
| 18 | + |
| 19 | +import javax.ws.rs.GET; |
| 20 | +import javax.ws.rs.Path; |
| 21 | +import javax.ws.rs.WebApplicationException; |
| 22 | +import javax.ws.rs.client.Client; |
| 23 | +import javax.ws.rs.client.ClientBuilder; |
| 24 | +import javax.ws.rs.core.Application; |
| 25 | +import javax.ws.rs.core.NewCookie; |
| 26 | +import javax.ws.rs.core.Response; |
| 27 | +import java.net.URI; |
| 28 | +import java.util.concurrent.atomic.AtomicReference; |
| 29 | + |
| 30 | +import static junit.framework.TestCase.assertEquals; |
| 31 | +import static junit.framework.TestCase.assertFalse; |
| 32 | +import static junit.framework.TestCase.assertNull; |
| 33 | +import org.glassfish.jersey.CommonProperties; |
| 34 | +import org.glassfish.jersey.client.ClientProperties; |
| 35 | +import org.glassfish.jersey.server.ResourceConfig; |
| 36 | +import org.glassfish.jersey.test.JerseyTest; |
| 37 | +import org.junit.AfterClass; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests ignoring of client responses in exceptions. |
| 43 | + * |
| 44 | + * @author Santiago Pericas-Geertsen |
| 45 | + */ |
| 46 | +public class IgnoreExceptionResponseTest extends JerseyTest { |
| 47 | + |
| 48 | + static String lastAllowSystemProperties; |
| 49 | + static String lastIgnoreExceptionResponse; |
| 50 | + static AtomicReference<URI> baseUri = new AtomicReference<>(); |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Application configure() { |
| 54 | + return new ResourceConfig(TestResource.class); |
| 55 | + } |
| 56 | + |
| 57 | + public IgnoreExceptionResponseTest() { |
| 58 | + baseUri.set(getBaseUri()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets ignore exception response as system property after enabling the provider. |
| 63 | + */ |
| 64 | + @BeforeClass |
| 65 | + public static void startUp() { |
| 66 | + lastAllowSystemProperties = System.setProperty(CommonProperties.ALLOW_SYSTEM_PROPERTIES_PROVIDER, "true"); |
| 67 | + lastIgnoreExceptionResponse = System.setProperty(ClientProperties.IGNORE_EXCEPTION_RESPONSE, "true"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Restores state after completion. |
| 72 | + */ |
| 73 | + @AfterClass |
| 74 | + public static void cleanUp() { |
| 75 | + if (lastIgnoreExceptionResponse != null) { |
| 76 | + System.setProperty(ClientProperties.IGNORE_EXCEPTION_RESPONSE, lastIgnoreExceptionResponse); |
| 77 | + } |
| 78 | + if (lastAllowSystemProperties != null) { |
| 79 | + System.setProperty(CommonProperties.ALLOW_SYSTEM_PROPERTIES_PROVIDER, lastAllowSystemProperties); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void test() { |
| 85 | + Client client = ClientBuilder.newClient(); |
| 86 | + Response r = client.target(getBaseUri()) |
| 87 | + .path("test") |
| 88 | + .path("first") |
| 89 | + .request() |
| 90 | + .get(); |
| 91 | + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); |
| 92 | + assertNull(r.getHeaderString("confidential")); |
| 93 | + assertNull(r.getCookies().get("confidential")); |
| 94 | + assertFalse(r.hasEntity()); |
| 95 | + } |
| 96 | + |
| 97 | + @Path("test") |
| 98 | + public static class TestResource { |
| 99 | + |
| 100 | + @Path("first") |
| 101 | + @GET |
| 102 | + public String first() { |
| 103 | + Client client = ClientBuilder.newClient(); |
| 104 | + String entity = client.target(baseUri.get()) |
| 105 | + .path("test") |
| 106 | + .path("second") |
| 107 | + .request() |
| 108 | + .get(String.class); // WebApplicationException may be thrown |
| 109 | + return processEntity(entity); |
| 110 | + } |
| 111 | + |
| 112 | + @Path("second") |
| 113 | + @GET |
| 114 | + public String second() { |
| 115 | + throw new WebApplicationException( |
| 116 | + "Leaking confidential information", |
| 117 | + Response.status(500) |
| 118 | + .header("confidential", "nuke-codes") |
| 119 | + .cookie(NewCookie.valueOf("confidential=more-nuke-codes")) |
| 120 | + .entity("even-more-nuke-codes") |
| 121 | + .build()); |
| 122 | + } |
| 123 | + |
| 124 | + private String processEntity(String entity) { |
| 125 | + return entity; // filter confidential information |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments