Example usage for org.apache.commons.httpclient HttpMethod getURI

List of usage examples for org.apache.commons.httpclient HttpMethod getURI

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getURI.

Prototype

public abstract URI getURI() throws URIException;

Source Link

Usage

From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java

private HttpMethod executeMethod(HttpMethod httpMethod) {
    for (Header header : this.headers.getHeaders()) {
        httpMethod.setRequestHeader(header);
    }//from w  w  w  .  jav a2 s.  c  om

    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new KsfxHttpRetryHandler(retryCount, retryDelay));

    try {
        int tryCount = 0;
        int statusCode;
        do {
            if (tryCount > 1) {
                httpMethod = createNewHttpMethod(httpMethod);
                try {
                    if (retryDelay == 0) {
                        retryDelay = DEFAULT_RETRY_DELAY;
                    }
                    Thread.sleep(retryDelay);
                } catch (InterruptedException e) {
                    logger.severe("InterruptedException");
                }
            }

            //PROXY Configuration
            /*
            if (torify) {
                    
            String proxyHost = "";
            Integer proxyPort = 0;
                    
            try {
                    proxyHost = SpiderConfiguration.getConfiguration().getString("torifyHost");
                    proxyPort = SpiderConfiguration.getConfiguration().getInt("torifyPort");
            } catch (Exception e) {
                logger.severe("Cannot get Proxy information");
            }
                    
            this.httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
            }
            */

            statusCode = this.httpClient.executeMethod(httpMethod);
            tryCount++;
        } while (!(statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_FORBIDDEN
                || statusCode == HttpStatus.SC_NOT_FOUND) && tryCount < retryCount);
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("HTTP method failed: " + httpMethod.getStatusLine() + " - "
                    + httpMethod.getURI().toString());
        }
    } catch (HttpException e) {
        e.printStackTrace();
        httpMethod.abort();
        try {
            logger.log(Level.SEVERE, "Redirrex " + e.getClass(), e);
            if (e.getClass().equals(RedirectException.class)) {
                logger.log(Level.SEVERE, "Is real redirect exception", e);
                throw new RuntimeException("HttpRedirectException");
            }
            logger.log(Level.SEVERE, "HTTP protocol error for URL: " + httpMethod.getURI().toString(), e);
        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);
        }
        throw new RuntimeException("HttpException");
    } catch (IOException e) {
        try {
            e.printStackTrace();
            logger.log(Level.SEVERE, "HTTP transport error for URL: " + httpMethod.getURI().toString(), e);

        } catch (URIException e1) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "URI exception", e);

        }
        throw new RuntimeException("IOException");
    }
    return httpMethod;
}

From source file:com.cloudbees.api.BeesClient.java

/**
 * Executes an HTTP method./*from  w w  w .  j ava2 s. c om*/
 */
private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException {
    BeesClientConfiguration conf = getBeesClientConfiguration();
    HttpClient httpClient = HttpClientHelper.createClient(conf);

    if (encodedAccountAuthorization != null)
        httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization);

    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpMethod.setRequestHeader(entry.getKey(), entry.getValue());
        }
    }

    int status = 500;
    String rsp = "Error";
    try {
        status = httpClient.executeMethod(httpMethod);
        rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream());
    } catch (IOException e) {
        throw (IOException) new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI()
                + " : code=" + status + " response=" + e.getMessage()).initCause(e);
    } finally {
        httpMethod.releaseConnection();
    }

    trace(status + ": " + rsp);

    return new HttpReply(httpMethod, status, rsp);
}

From source file:davmail.exchange.ews.EwsExchangeSession.java

@Override
protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password)
        throws IOException {
    LOGGER.debug("Form based authentication detected");

    HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
    if (logonMethod == null) {
        LOGGER.debug(//  ww  w .  j a  v  a2  s. c o m
                "Authentication form not found at " + initmethod.getURI() + ", will try direct EWS access");
    } else {
        logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);
    }

    return logonMethod;
}

From source file:io.hops.hopsworks.api.jobs.JobService.java

/**
 * Get the job ui for the specified job.
 * This act as a proxy to get the job ui from yarn
 * <p>// w w  w.  j  a  v a2 s . co m
 * @param appId
 * @param param
 * @param sc
 * @param req
 * @return
 */
@GET
@Path("/{appId}/prox/{path: .+}")
@Produces(MediaType.WILDCARD)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
public Response getProxy(@PathParam("appId") final String appId, @PathParam("path") final String param,
        @Context SecurityContext sc, @Context HttpServletRequest req) {

    Response response = checkAccessRight(appId);
    if (response != null) {
        return response;
    }
    try {
        String trackingUrl;
        if (param.matches("http([a-zA-Z,:,/,.,0-9,-])+:([0-9])+(.)+")) {
            trackingUrl = param;
        } else {
            trackingUrl = "http://" + param;
        }
        trackingUrl = trackingUrl.replace("@hwqm", "?");
        if (!hasAppAccessRight(trackingUrl)) {
            LOGGER.log(Level.SEVERE, "A user is trying to access an app outside their project!");
            return Response.status(Response.Status.FORBIDDEN).build();
        }
        org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(trackingUrl, false);

        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);

        final HttpMethod method = new GetMethod(uri.getEscapedURI());
        Enumeration<String> names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = req.getHeader(name);
            if (PASS_THROUGH_HEADERS.contains(name)) {
                //yarn does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (!name.toLowerCase().equals("accept-encoding") || trackingUrl.contains(".js")) {
                    method.setRequestHeader(name, value);
                }
            }
        }
        String user = req.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(method);
        Response.ResponseBuilder responseBuilder = noCacheResponse
                .getNoCacheResponseBuilder(Response.Status.OK);
        for (Header header : method.getResponseHeaders()) {
            responseBuilder.header(header.getName(), header.getValue());
        }
        //method.getPath().contains("/allexecutors") is needed to replace the links under Executors tab
        //which are in a json response object
        if (method.getResponseHeader("Content-Type") == null
                || method.getResponseHeader("Content-Type").getValue().contains("html")
                || method.getPath().contains("/allexecutors")) {
            final String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort();
            if (method.getResponseHeader("Content-Length") == null) {
                responseBuilder.entity(new StreamingOutput() {
                    @Override
                    public void write(OutputStream out) throws IOException, WebApplicationException {
                        Writer writer = new BufferedWriter(new OutputStreamWriter(out));
                        InputStream stream = method.getResponseBodyAsStream();
                        Reader in = new InputStreamReader(stream, "UTF-8");
                        char[] buffer = new char[4 * 1024];
                        String remaining = "";
                        int n;
                        while ((n = in.read(buffer)) != -1) {
                            StringBuilder strb = new StringBuilder();
                            strb.append(buffer, 0, n);
                            String s = remaining + strb.toString();
                            remaining = s.substring(s.lastIndexOf(">") + 1, s.length());
                            s = hopify(s.substring(0, s.lastIndexOf(">") + 1), param, appId, source);
                            writer.write(s);
                        }
                        writer.flush();
                    }
                });
            } else {
                String s = hopify(method.getResponseBodyAsString(), param, appId, source);
                responseBuilder.entity(s);
                responseBuilder.header("Content-Length", s.length());
            }

        } else {
            responseBuilder.entity(new StreamingOutput() {
                @Override
                public void write(OutputStream out) throws IOException, WebApplicationException {
                    InputStream stream = method.getResponseBodyAsStream();
                    org.apache.hadoop.io.IOUtils.copyBytes(stream, out, 4096, true);
                    out.flush();
                }
            });
        }
        return responseBuilder.build();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "exception while geting job ui " + e.getLocalizedMessage(), e);
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).build();
    }

}

From source file:davmail.exchange.ews.EwsExchangeSession.java

/**
 * Check endpoint url./*from  w  ww  .ja v  a  2  s  . co  m*/
 *
 * @param endPointUrl endpoint url
 * @throws IOException on error
 */
protected void checkEndPointUrl(String endPointUrl) throws IOException {
    HttpMethod getMethod = new GetMethod(endPointUrl);
    getMethod.setFollowRedirects(false);
    try {
        int status = DavGatewayHttpClientFacade.executeNoRedirect(httpClient, getMethod);
        if (status == HttpStatus.SC_UNAUTHORIZED) {
            throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
        } else if (status != HttpStatus.SC_MOVED_TEMPORARILY) {
            throw DavGatewayHttpClientFacade.buildHttpException(getMethod);
        }
        // check Location
        Header locationHeader = getMethod.getResponseHeader("Location");
        if (locationHeader == null || !"/ews/services.wsdl".equalsIgnoreCase(locationHeader.getValue())) {
            throw new IOException("Ews endpoint not available at " + getMethod.getURI().toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:davmail.exchange.dav.DavExchangeSession.java

protected void fixClientHost(HttpMethod method) {
    try {/*w w w  . j a  v  a2s .co m*/
        // update client host, workaround for Exchange 2003 mailbox with an Exchange 2007 frontend
        URI currentUri = method.getURI();
        if (currentUri != null && currentUri.getHost() != null && currentUri.getScheme() != null) {
            httpClient.getHostConfiguration().setHost(currentUri.getHost(), currentUri.getPort(),
                    currentUri.getScheme());
        }
    } catch (URIException e) {
        LOGGER.warn("Unable to update http client host:" + e.getMessage(), e);
    }
}

From source file:davmail.exchange.ExchangeSession.java

protected String getAbsoluteUri(HttpMethod method, String path) throws URIException {
    URI uri = method.getURI();
    if (path != null) {
        // reset query string
        uri.setQuery(null);/*from ww w. j a  v a 2  s  .  com*/
        if (path.startsWith("/")) {
            // path is absolute, replace method path
            uri.setPath(path);
        } else if (path.startsWith("http://") || path.startsWith("https://")) {
            return path;
        } else {
            // relative path, build new path
            String currentPath = method.getPath();
            int end = currentPath.lastIndexOf('/');
            if (end >= 0) {
                uri.setPath(currentPath.substring(0, end + 1) + path);
            } else {
                throw new URIException(uri.getURI());
            }
        }
    }
    return uri.getURI();
}

From source file:davmail.exchange.ExchangeSession.java

protected String getScriptBasedFormURL(HttpMethod initmethod, String pathQuery) throws URIException {
    URI initmethodURI = initmethod.getURI();
    int queryIndex = pathQuery.indexOf('?');
    if (queryIndex >= 0) {
        if (queryIndex > 0) {
            // update path
            String newPath = pathQuery.substring(0, queryIndex);
            if (newPath.startsWith("/")) {
                // absolute path
                initmethodURI.setPath(newPath);
            } else {
                String currentPath = initmethodURI.getPath();
                int folderIndex = currentPath.lastIndexOf('/');
                if (folderIndex >= 0) {
                    // replace relative path
                    initmethodURI.setPath(currentPath.substring(0, folderIndex + 1) + newPath);
                } else {
                    // should not happen
                    initmethodURI.setPath('/' + newPath);
                }/*from ww  w. j a v a  2s.  c  o  m*/
            }
        }
        initmethodURI.setQuery(pathQuery.substring(queryIndex + 1));
    }
    return initmethodURI.getURI();
}

From source file:davmail.exchange.dav.DavExchangeSession.java

protected void buildMailPath(HttpMethod method) throws DavMailAuthenticationException {
    // get mailPath from welcome page on Exchange 2003
    mailPath = getMailpathFromWelcomePage(method);

    //noinspection VariableNotUsedInsideIf
    if (mailPath != null) {
        // Exchange 2003
        serverVersion = "Exchange2003";
        fixClientHost(method);/*www  .jav a 2 s  . c om*/
        checkPublicFolder();
        try {
            buildEmail(method.getURI().getHost());
        } catch (URIException uriException) {
            LOGGER.warn(uriException);
        }
    } else {
        // Exchange 2007 : get alias and email from options page
        serverVersion = "Exchange2007";

        // Gallookup is an Exchange 2003 only feature
        disableGalLookup = true;
        fixClientHost(method);
        getEmailAndAliasFromOptions();

        checkPublicFolder();

        // failover: try to get email through Webdav and Galfind
        if (alias == null || email == null) {
            try {
                buildEmail(method.getURI().getHost());
            } catch (URIException uriException) {
                LOGGER.warn(uriException);
            }
        }

        // build standard mailbox link with email
        mailPath = "/exchange/" + email + '/';
    }

    if (mailPath == null || email == null) {
        throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED_PASSWORD_EXPIRED");
    }
    LOGGER.debug("Current user email is " + email + ", alias is " + alias + ", mailPath is " + mailPath + " on "
            + serverVersion);
    rootPath = mailPath.substring(0, mailPath.lastIndexOf('/', mailPath.length() - 2) + 1);
}

From source file:davmail.exchange.ExchangeSession.java

protected HttpMethod formLogin(HttpClient httpClient, HttpMethod initmethod, String userName, String password)
        throws IOException {
    LOGGER.debug("Form based authentication detected");

    HttpMethod logonMethod = buildLogonMethod(httpClient, initmethod);
    if (logonMethod == null) {
        LOGGER.debug("Authentication form not found at " + initmethod.getURI() + ", trying default url");
        logonMethod = new PostMethod("/owa/auth/owaauth.dll");
    }//from  w ww  . j ava 2  s . co  m
    logonMethod = postLogonMethod(httpClient, logonMethod, userName, password);

    return logonMethod;
}