Search in sources :

Example 1 with HttpRequest

use of org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest in project maven-plugins by apache.

the class AbstractSiteDeployWebDavTest method davDeployThruProxyWitAuthzInProxy.

@Test
public void davDeployThruProxyWitAuthzInProxy() throws Exception {
    FileUtils.cleanDirectory(siteTargetPath);
    //SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler( siteTargetPath );
    Map<String, String> authentications = new HashMap<String, String>();
    authentications.put("foo", "titi");
    AuthAsyncProxyServlet servlet = new AuthAsyncProxyServlet(authentications, siteTargetPath);
    SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(servlet);
    try {
        File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
        AbstractMojo mojo = getMojo(pluginXmlFile);
        assertNotNull(mojo);
        SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
        siteMavenProjectStub.getDistributionManagement().getSite().setUrl("dav:http://toto.com/site/");
        setVariableValueToObject(mojo, "project", siteMavenProjectStub);
        Settings settings = new Settings();
        Proxy proxy = new Proxy();
        //dummy proxy
        proxy.setActive(true);
        proxy.setHost("localhost");
        proxy.setPort(simpleDavServerHandler.getPort());
        proxy.setProtocol("dav");
        proxy.setUsername("foo");
        proxy.setPassword("titi");
        proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
        settings.addProxy(proxy);
        setVariableValueToObject(mojo, "settings", settings);
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setProxies(Arrays.asList(proxy));
        MavenSession mavenSession = new MavenSession(getContainer(), null, request, null);
        setVariableValueToObject(mojo, "mavenSession", mavenSession);
        File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
        // test which mojo we are using
        if (ReflectionUtils.getFieldByNameIncludingSuperclasses("inputDirectory", mojo.getClass()) != null) {
            setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
        } else {
            ArtifactRepositoryFactory artifactRepositoryFactory = getContainer().lookup(ArtifactRepositoryFactory.class);
            setVariableValueToObject(mojo, "stagingDirectory", inputDirectory);
            setVariableValueToObject(mojo, "reactorProjects", Collections.emptyList());
            setVariableValueToObject(mojo, "localRepository", artifactRepositoryFactory.createArtifactRepository("local", "foo", "default", null, null));
            setVariableValueToObject(mojo, "siteTool", getContainer().lookup(SiteTool.class));
            setVariableValueToObject(mojo, "siteDirectory", new File("foo"));
            setVariableValueToObject(mojo, "repositories", Collections.emptyList());
        }
        mojo.execute();
        assertContentInFiles();
        assertTrue(requestsContainsProxyUse(servlet.httpRequests));
        assertAtLeastOneRequestContainsHeader(servlet.httpRequests, "Proxy-Authorization");
        for (HttpRequest rq : servlet.httpRequests) {
            log.info(rq.toString());
        }
    } finally {
        simpleDavServerHandler.stop();
    }
}
Also used : HttpRequest(org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest) SiteMavenProjectStub(org.apache.maven.plugins.site.stubs.SiteMavenProjectStub) HashMap(java.util.HashMap) SiteTool(org.apache.maven.doxia.tools.SiteTool) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) AbstractMojo(org.apache.maven.plugin.AbstractMojo) ArtifactRepositoryFactory(org.apache.maven.artifact.repository.ArtifactRepositoryFactory) MavenSession(org.apache.maven.execution.MavenSession) Proxy(org.apache.maven.settings.Proxy) File(java.io.File) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Example 2 with HttpRequest

use of org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest in project maven-plugins by apache.

the class AbstractSiteDeployWebDavTest method davDeployThruProxyWithoutAuthzInProxy.

@Test
public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {
    FileUtils.cleanDirectory(siteTargetPath);
    SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
    try {
        File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
        AbstractMojo mojo = getMojo(pluginXmlFile);
        assertNotNull(mojo);
        SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
        // olamy, Note : toto is something like foo or bar for french folks :-)
        String siteUrl = "dav:http://toto.com/site/";
        siteMavenProjectStub.getDistributionManagement().getSite().setUrl(siteUrl);
        setVariableValueToObject(mojo, "project", siteMavenProjectStub);
        Settings settings = new Settings();
        Proxy proxy = new Proxy();
        //dummy proxy
        proxy.setActive(true);
        proxy.setHost("localhost");
        proxy.setPort(simpleDavServerHandler.getPort());
        proxy.setProtocol("http");
        proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
        settings.addProxy(proxy);
        setVariableValueToObject(mojo, "settings", settings);
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setProxies(Arrays.asList(proxy));
        MavenSession mavenSession = new MavenSession(getContainer(), null, request, null);
        setVariableValueToObject(mojo, "mavenSession", mavenSession);
        File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
        setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
        mojo.execute();
        assertContentInFiles();
        assertTrue(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
        for (HttpRequest rq : simpleDavServerHandler.httpRequests) {
            log.info(rq.toString());
        }
    } finally {
        simpleDavServerHandler.stop();
    }
}
Also used : HttpRequest(org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest) SiteMavenProjectStub(org.apache.maven.plugins.site.stubs.SiteMavenProjectStub) MavenSession(org.apache.maven.execution.MavenSession) Proxy(org.apache.maven.settings.Proxy) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) AbstractMojo(org.apache.maven.plugin.AbstractMojo) File(java.io.File) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Example 3 with HttpRequest

use of org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest in project maven-plugins by apache.

the class AuthAsyncProxyServlet method service.

/** {@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!");
            }
            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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest) Enumeration(java.util.Enumeration) HttpServletResponse(javax.servlet.http.HttpServletResponse) File(java.io.File)

Aggregations

File (java.io.File)3 HttpRequest (org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest)3 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)2 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)2 MavenSession (org.apache.maven.execution.MavenSession)2 AbstractMojo (org.apache.maven.plugin.AbstractMojo)2 SiteMavenProjectStub (org.apache.maven.plugins.site.stubs.SiteMavenProjectStub)2 Proxy (org.apache.maven.settings.Proxy)2 Settings (org.apache.maven.settings.Settings)2 Test (org.junit.Test)2 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ArtifactRepositoryFactory (org.apache.maven.artifact.repository.ArtifactRepositoryFactory)1 SiteTool (org.apache.maven.doxia.tools.SiteTool)1