|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Banco do Brasil S/A. 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 | + * Contributors: |
| 16 | + * Marcelo Rubim |
| 17 | + */ |
| 18 | +package org.glassfish.jersey.jetty.connector; |
| 19 | + |
| 20 | +import org.eclipse.jetty.server.Request; |
| 21 | +import org.eclipse.jetty.server.Server; |
| 22 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 23 | +import org.glassfish.jersey.client.ClientConfig; |
| 24 | +import org.glassfish.jersey.client.ClientProperties; |
| 25 | +import org.glassfish.jersey.server.ResourceConfig; |
| 26 | +import org.glassfish.jersey.test.JerseyTest; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import javax.servlet.ServletException; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import javax.servlet.http.HttpServletResponse; |
| 32 | +import javax.ws.rs.GET; |
| 33 | +import javax.ws.rs.Path; |
| 34 | +import javax.ws.rs.core.Application; |
| 35 | +import javax.ws.rs.core.Response; |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.charset.Charset; |
| 38 | +import java.util.Base64; |
| 39 | + |
| 40 | +import static org.junit.Assert.assertEquals; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Marcelo Rubim |
| 44 | +\ */ |
| 45 | +public class ProxyTest extends JerseyTest { |
| 46 | + private static final Charset CHARACTER_SET = Charset.forName("iso-8859-1"); |
| 47 | + private static final String PROXY_URI = "http://127.0.0.1:9997"; |
| 48 | + private static final String PROXY_USERNAME = "proxy-user"; |
| 49 | + private static final String PROXY_PASSWORD = "proxy-password"; |
| 50 | + |
| 51 | + |
| 52 | + @Path("") |
| 53 | + public static class ProxyResource { |
| 54 | + |
| 55 | + @GET |
| 56 | + public Response getProxy() { |
| 57 | + return Response.status(407).header("Proxy-Authenticate", "Basic").build(); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Path("proxyTest") |
| 63 | + public static class ProxyTestResource { |
| 64 | + |
| 65 | + @GET |
| 66 | + public Response getOK() { |
| 67 | + return Response.ok().build(); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected Application configure() { |
| 74 | + ResourceConfig config = new ResourceConfig(ProxyResource.class, ProxyTestResource.class); |
| 75 | + return config; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void configureClient(ClientConfig config) { |
| 80 | + config.connectorProvider(new JettyConnectorProvider()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGet407() { |
| 85 | + startFakeProxy(); |
| 86 | + client().property(ClientProperties.PROXY_URI, ProxyTest.PROXY_URI); |
| 87 | + Response response = target("proxyTest").request().get(); |
| 88 | + assertEquals(407, response.getStatus()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testGetSuccess() { |
| 93 | + startFakeProxy(); |
| 94 | + client().property(ClientProperties.PROXY_URI, ProxyTest.PROXY_URI); |
| 95 | + client().property(ClientProperties.PROXY_USERNAME, ProxyTest.PROXY_USERNAME); |
| 96 | + client().property(ClientProperties.PROXY_PASSWORD, ProxyTest.PROXY_PASSWORD); |
| 97 | + Response response = target("proxyTest").request().get(); |
| 98 | + assertEquals(200, response.getStatus()); |
| 99 | + } |
| 100 | + |
| 101 | + private void startFakeProxy(){ |
| 102 | + Server server = new Server(9997); |
| 103 | + server.setHandler(new ProxyHandler()); |
| 104 | + try { |
| 105 | + server.start(); |
| 106 | + } catch (Exception e) { |
| 107 | + |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + class ProxyHandler extends AbstractHandler { |
| 112 | + @Override |
| 113 | + public void handle(String target, |
| 114 | + Request baseRequest, |
| 115 | + HttpServletRequest request, |
| 116 | + HttpServletResponse response) throws IOException, |
| 117 | + ServletException { |
| 118 | + |
| 119 | + if (request.getHeader("Proxy-Authorization") != null) { |
| 120 | + String proxyAuthorization = request.getHeader("Proxy-Authorization"); |
| 121 | + String decoded = new String(Base64.getDecoder().decode(proxyAuthorization.substring(6).getBytes()), |
| 122 | + CHARACTER_SET); |
| 123 | + final String[] split = decoded.split(":"); |
| 124 | + final String username = split[0]; |
| 125 | + final String password = split[1]; |
| 126 | + |
| 127 | + if (!username.equals(PROXY_USERNAME)) { |
| 128 | + response.setStatus(400); |
| 129 | + System.out.println("Found unexpected username: " + username); |
| 130 | + } |
| 131 | + |
| 132 | + if (!password.equals(PROXY_PASSWORD)) { |
| 133 | + response.setStatus(400); |
| 134 | + System.out.println("Found unexpected password: " + username); |
| 135 | + } |
| 136 | + response.setStatus(200); |
| 137 | + //TODO Add redirect to requestURI |
| 138 | + } else { |
| 139 | + response.setStatus(407); |
| 140 | + response.addHeader("Proxy-Authenticate", "Basic"); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + baseRequest.setHandled(true); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments