Example usage for javax.servlet.http HttpServletResponse SC_PROXY_AUTHENTICATION_REQUIRED

List of usage examples for javax.servlet.http HttpServletResponse SC_PROXY_AUTHENTICATION_REQUIRED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_PROXY_AUTHENTICATION_REQUIRED.

Prototype

int SC_PROXY_AUTHENTICATION_REQUIRED

To view the source code for javax.servlet.http HttpServletResponse SC_PROXY_AUTHENTICATION_REQUIRED.

Click Source Link

Document

Status code (407) indicating that the client MUST first authenticate itself with the proxy.

Usage

From source file:io.fabric8.agent.download.DownloadManagerTest.java

@Test
public void testDownloadUsingAuthenticatedProxy() throws Exception {
    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {
        @Override/*from w w  w.ja  va  2s.  c  om*/
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String proxyAuth = request.getHeader("Proxy-Authorization");
            if (proxyAuth == null || proxyAuth.trim().equals("")) {
                response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
                response.addHeader("Proxy-Authenticate", "Basic realm=\"Proxy Server\"");
                baseRequest.setHandled(true);
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[] { 0x42 });
                response.getOutputStream().close();
            }
        }
    });
    server.start();

    Properties custom = new Properties();
    custom.setProperty("org.ops4j.pax.url.mvn.proxySupport", "true");
    String settings = createMavenSettingsWithProxy(server.getConnectors()[0].getLocalPort());
    DownloadManager dm = createDownloadManager("http://relevant.not/maven2@id=central", settings, custom);

    try {
        StreamProvider df = download(dm, "mvn:x.y/z/1.0");
        assertNotNull(df);
        assertNotNull(df.getUrl());
        assertNotNull(df.getFile());
        assertEquals("z-1.0.jar", df.getFile().getName());
        LOG.info("Downloaded URL={}, FILE={}", df.getUrl(), df.getFile());
    } finally {
        server.stop();
    }
}

From source file:io.fabric8.agent.DownloadManagerTest.java

@Test
public void testDownloadUsingAuthenticatedProxy() throws Exception {
    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {
        @Override//from ww w.  j av a2  s.co  m
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String proxyAuth = request.getHeader("Proxy-Authorization");
            if (proxyAuth == null || proxyAuth.trim().equals("")) {
                response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
                response.addHeader("Proxy-Authenticate", "Basic realm=\"Proxy Server\"");
                baseRequest.setHandled(true);
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[] { 0x42 });
                response.getOutputStream().close();
            }
        }
    });
    server.start();

    Properties custom = new Properties();
    custom.setProperty("org.ops4j.pax.url.mvn.proxySupport", "true");
    String settings = createMavenSettingsWithProxy(server.getConnectors()[0].getLocalPort());
    DownloadManager dm = createDownloadManager("http://relevant.not/maven2@id=central", settings, custom);

    try {
        final CountDownLatch latch = new CountDownLatch(1);
        DownloadFuture df = dm.download("mvn:x.y/z/1.0");
        df.addListener(new FutureListener<DownloadFuture>() {
            @Override
            public void operationComplete(DownloadFuture future) {
                latch.countDown();
            }
        });

        latch.await(30, TimeUnit.SECONDS);
        assertNotNull(df.getUrl());
        assertNotNull(df.getFile());
        assertEquals("z-1.0.jar", df.getFile().getName());
        LOG.info("Downloaded URL={}, FILE={}", df.getUrl(), df.getFile());
    } finally {
        server.stop();
    }
}

From source file:io.fabric8.maven.impl.MavenProxyServletSupportTest.java

@Test
@Ignore("[https://jira.codehaus.org/browse/WAGON-416][FABRIC-171] Wait for wagon-http-lightweight fixes")
public void testDownloadUsingAuthenticatedProxy() throws Exception {
    testDownload(new AbstractHandler() {
        @Override//from w  w w  . ja v  a 2  s . com
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String proxyAuth = request.getHeader("Proxy-Authorization");
            if (proxyAuth == null || proxyAuth.trim().equals("")) {
                response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
                response.addHeader("Proxy-Authenticate", "Basic realm=\"Proxy Server\"");
                baseRequest.setHandled(true);
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[] { 0x42 });
                response.getOutputStream().close();
            }
        }
    });
}

From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java

@Test
public void testDownloadUsingAuthenticatedProxy() throws Exception {
    testDownload(new AbstractHandler() {
        @Override/*from  w  ww .j  a va  2  s.c  om*/
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            String proxyAuth = request.getHeader("Proxy-Authorization");
            if (proxyAuth == null || proxyAuth.trim().equals("")) {
                response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
                response.addHeader("Proxy-Authenticate", "Basic realm=\"Proxy Server\"");
                baseRequest.setHandled(true);
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[] { 0x42 });
                response.getOutputStream().close();
            }
        }
    });
}

From source file:org.apache.maven.plugins.site.AuthAsyncProxyServlet.java

/** {@inheritDoc} */
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;

    log.info("handle ");

    if (this.authentications != null && !this.authentications.isEmpty()) {
        String proxyAuthorization = request.getHeader("Proxy-Authorization");
        if (proxyAuthorization != null && proxyAuthorization.startsWith("Basic ")) {
            String proxyAuth = proxyAuthorization.substring(6);
            String authorization = B64Code.decode(proxyAuth);
            String[] authTokens = authorization.split(":");
            String user = authTokens[0];
            String password = authTokens[1];

            if (this.authentications.get(user) == null) {
                throw new IllegalArgumentException(user + " not found in the map!");
            }/*from ww  w.j a v  a2  s.co m*/

            if (sleepTime > 0) {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    // nop
                }
            }
            String authPass = this.authentications.get(user);
            if (password.equals(authPass)) {
                String targetPath = request.getServletPath();

                HttpRequest rq = new HttpRequest();
                rq.method = request.getMethod();
                rq.path = targetPath;

                @SuppressWarnings("rawtypes")
                Enumeration headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()) {
                    String name = (String) headerNames.nextElement();
                    rq.headers.put(name, request.getHeader(name));
                }

                httpRequests.add(rq);

                if (request.getMethod().equalsIgnoreCase("PUT") && targetPath != null) {
                    File targetFile = new File(siteTargetPath, targetPath);
                    log.info("writing file " + targetFile.getPath());
                    FileUtils.writeByteArrayToFile(targetFile, IOUtils.toByteArray(request.getInputStream()));
                }

                //PrintWriter writer = response.getWriter();

                response.setStatus(HttpServletResponse.SC_OK);
                return;
            }
        }

        // Proxy-Authenticate Basic realm="CCProxy Authorization"
        response.addHeader("Proxy-Authenticate", "Basic realm=\"Jetty Proxy Authorization\"");
        response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
        return;
    }

    super.service(req, res);
}

From source file:org.apache.maven.wagon.tck.http.fixture.ProxyAuthenticationFilter.java

public void doFilter(final ServletRequest req, final ServletResponse resp, final FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    String header = request.getHeader("Proxy-Authorization");
    if (header == null) {
        response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
        response.addHeader("Proxy-Authenticate", "Basic realm=\"Squid proxy-caching web server\"");
        return;/*from ww w.j  a v a 2s  .co m*/
    } else {
        String data = header.substring("BASIC ".length());
        data = new String(Base64.decodeBase64(data));
        String[] creds = data.split(":");

        if (!creds[0].equals(username) || !creds[1].equals(password)) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
    }

    chain.doFilter(req, resp);
}

From source file:org.archive.wayback.accesspoint.proxy.AuthProxyConfigSelector.java

public boolean selectConfigHandler(HttpServletRequest request, HttpServletResponse response,
        ProxyAccessPoint proxy) throws IOException {
    response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED); //407
    response.setHeader("Proxy-Authenticate", "Basic realm=\"" + authMsg + "\"");
    response.setContentType("text/html");

    //TODO: Better way to pass this to jsp?
    request.setAttribute("proxyAccessPoint", proxy);

    StringHttpServletResponseWrapper wrappedResponse = new StringHttpServletResponseWrapper(response);
    RequestDispatcher dispatcher = request.getRequestDispatcher(proxyInfoJsp);

    try {//from ww  w. ja  va  2 s .  c om
        dispatcher.forward(request, wrappedResponse);
    } catch (ServletException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    PrintWriter writer = response.getWriter();
    writer.println(wrappedResponse.getStringResponse());
    return true;
}