Example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Prototype

int SC_UNAUTHORIZED

To view the source code for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Click Source Link

Document

<tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:com.cloud.network.nicira.NiciraNvpApi.java

private void executeMethod(HttpMethodBase method) throws NiciraNvpApiException {
    try {/*www.  ja v a2s .  c om*/
        _client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            method.releaseConnection();
            // login and try again
            login();
            _client.executeMethod(method);
        }
    } catch (HttpException e) {
        s_logger.error("HttpException caught while trying to connect to the Nicira NVP Controller", e);
        throw new NiciraNvpApiException("API call to Nicira NVP Controller Failed", e);
    } catch (IOException e) {
        s_logger.error("IOException caught while trying to connect to the Nicira NVP Controller", e);
        throw new NiciraNvpApiException("API call to Nicira NVP Controller Failed", e);
    }
}

From source file:es.juntadeandalucia.mapea.proxy.ProxyRedirect.java

/***************************************************************************
 * Process the HTTP Post request/*from   w ww . ja  v a  2s. co m*/
 ***************************************************************************/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    boolean checkedContent = false;
    boolean legend = false;
    String strErrorMessage = "";
    String serverUrl = request.getParameter("url");
    log.info("POST param serverUrl: " + serverUrl);
    if (serverUrl.startsWith("legend")) {
        serverUrl = serverUrl.replace("legend", "");
        serverUrl = serverUrl.replace("?", "&");
        serverUrl = serverUrl.replaceFirst("&", "?");
        legend = true;
    }
    serverUrl = checkTypeRequest(serverUrl);
    // log.info("serverUrl ckecked: " + serverUrl);
    if (!serverUrl.equals("ERROR")) {
        if (serverUrl.startsWith("http://") || serverUrl.startsWith("https://")) {
            PostMethod httppost = null;
            try {
                if (log.isDebugEnabled()) {
                    Enumeration<?> e = request.getHeaderNames();
                    while (e.hasMoreElements()) {
                        String name = (String) e.nextElement();
                        String value = request.getHeader(name);
                        log.debug("request header:" + name + ":" + value);
                    }
                }
                HttpClient client = new HttpClient();
                httppost = new PostMethod(serverUrl);
                // PATH
                httppost.setDoAuthentication(false);
                httppost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        new DefaultHttpMethodRetryHandler(3, false));
                // FIN_PATH
                // PATH_MAPEAEDITA_SECURITY - AP
                // PATCH_TICKET_MJM-20112405-POST
                String authorizationValue = request.getHeader(AUTHORIZATION); // ADD_SECURITY_20091210
                if (authorizationValue == null) {
                    // The 'Authorization' header must be in this form ->
                    // Authorization: Basic <encodedLogin>
                    // 'encodedLogin' is a string in the form 'user:pass'
                    // that has been encoded by way of the Base64 algorithm.
                    // More info on this can be found at
                    // http://en.wikipedia.org/wiki/Basic_access_authentication
                    String user = (String) request.getSession().getAttribute("user");
                    String pass = (String) request.getSession().getAttribute("pass");
                    if (user != null && pass != null) {
                        String userAndPass = user + ":" + pass;
                        String encodedLogin = new String(
                                org.apache.commons.codec.binary.Base64.encodeBase64(userAndPass.getBytes()));
                        httppost.addRequestHeader(AUTHORIZATION, "Basic " + encodedLogin);
                    } else { // MJM - 20110520
                        String ticketParameter = request.getParameter("ticket");
                        if (ticketParameter != null) {
                            ticketParameter = ticketParameter.trim();
                            if (!ticketParameter.isEmpty()) {
                                Ticket ticket = TicketFactory.createInstance();
                                try {
                                    Map<String, String> props = ticket.getProperties(ticketParameter);
                                    user = props.get("user");
                                    pass = props.get("pass");
                                    String userAndPass = user + ":" + pass;
                                    String encodedLogin = new String(org.apache.commons.codec.binary.Base64
                                            .encodeBase64(userAndPass.getBytes()));
                                    httppost.addRequestHeader(AUTHORIZATION, "Basic " + encodedLogin);
                                } catch (Exception e) {
                                    log.info("-------------------------------------------");
                                    log.info("EXCEPCTION THROWED BY PROXYREDIRECT CLASS");
                                    log.info("METHOD: doPost");
                                    log.info("TICKET VALUE: " + ticketParameter);
                                    log.info("-------------------------------------------");
                                }
                            }
                        }
                    }
                } else {
                    httppost.addRequestHeader(AUTHORIZATION, authorizationValue);
                }
                // FIN_PATH_TICKET_MJM-20112405-POST
                // FIN_PATH_MAPEAEDITA_SECURITY - AP
                String body = inputStreamAsString(request.getInputStream());
                StringRequestEntity bodyEntity = new StringRequestEntity(body, null, null);
                if (0 == httppost.getParameters().length) {
                    log.debug("No Name/Value pairs found ... pushing as received"); // PATCH
                    httppost.setRequestEntity(bodyEntity); // PATCH
                }
                if (log.isDebugEnabled()) {
                    log.debug("Body = " + body);
                    NameValuePair[] nameValuePairs = httppost.getParameters();
                    log.debug("NameValuePairs found: " + nameValuePairs.length);
                    for (int i = 0; i < nameValuePairs.length; ++i) {
                        log.debug("parameters:" + nameValuePairs[i].toString());
                    }
                }
                if (!legend)
                    client.getParams().setParameter("http.protocol.content-charset", "UTF-8");
                if (soap) {
                    httppost.addRequestHeader("SOAPAction", serverUrl);
                }
                client.executeMethod(httppost);
                // PATH_FOLLOW_REDIRECT_POST
                int j = 0;
                String redirectLocation;
                Header locationHeader = httppost.getResponseHeader("location");
                while (locationHeader != null && j < numMaxRedirects) {
                    redirectLocation = locationHeader.getValue();
                    // AGG 20111304 Aadimos el cuerpo de la peticin POST a
                    // la nueva peticin redirigida
                    // String bodyPost = httppost.getResponseBodyAsString();
                    StringRequestEntity bodyEntityPost = new StringRequestEntity(body, null, null);
                    httppost.releaseConnection();
                    httppost = new PostMethod(redirectLocation);
                    // AGG 20110912 Aadidas cabeceras peticin SOAP
                    if (redirectLocation.toLowerCase().contains("wsdl")) {
                        redirectLocation = serverUrl.replace("?wsdl", "");
                        httppost.addRequestHeader("SOAPAction", redirectLocation);
                        httppost.addRequestHeader("Content-type", "text/xml");
                    }
                    httppost.setRequestEntity(bodyEntityPost);
                    client.executeMethod(httppost);
                    locationHeader = httppost.getResponseHeader("location");
                    j++;
                }
                log.info("Number of followed redirections: " + j);
                if (locationHeader != null && j == numMaxRedirects) {
                    log.error("The maximum number of redirects (" + numMaxRedirects + ") is exceed.");
                }
                // FIN_PATH_FOLLOW_REDIRECT_POST
                if (log.isDebugEnabled()) {
                    Header[] responseHeaders = httppost.getResponseHeaders();
                    for (int i = 0; i < responseHeaders.length; ++i) {
                        String headerName = responseHeaders[i].getName();
                        String headerValue = responseHeaders[i].getValue();
                        log.debug("responseHeaders:" + headerName + "=" + headerValue);
                    }
                }
                // dump response to out
                if (httppost.getStatusCode() == HttpStatus.SC_OK) {
                    // PATH_SECURITY_PROXY - AG
                    Header[] respHeaders = httppost.getResponseHeaders();
                    int compSize = httppost.getResponseBody().length;
                    ArrayList<Header> headerList = new ArrayList<Header>(Arrays.asList(respHeaders));
                    String headersString = headerList.toString();
                    checkedContent = checkContent(headersString, compSize, serverUrl);
                    // FIN_PATH_SECURITY_PROXY - AG
                    if (checkedContent == true) {
                        /*
                         * checks if it has requested an getfeatureinfo to modify the response content
                         * type.
                         */
                        String requesteredUrl = request.getParameter("url");
                        if (GETINFO_PLAIN_REGEX.matcher(requesteredUrl).matches()) {
                            response.setContentType("text/plain");
                        } else if (GETINFO_GML_REGEX.matcher(requesteredUrl).matches()) {
                            response.setContentType("application/gml+xml");
                        } else if (GETINFO_HTML_REGEX.matcher(requesteredUrl).matches()) {
                            response.setContentType("text/html");
                        } else if (requesteredUrl.toLowerCase().contains("mapeaop=geosearch")
                                || requesteredUrl.toLowerCase().contains("mapeaop=geoprint")) {
                            response.setContentType("application/json");
                        } else {
                            response.setContentType("text/xml");
                        }
                        if (legend) {
                            String responseBody = httppost.getResponseBodyAsString();
                            if (responseBody.contains("ServiceExceptionReport")
                                    && serverUrl.contains("LegendGraphic")) {
                                response.sendRedirect("Componente/img/blank.gif");
                            } else {
                                response.setContentLength(responseBody.length());
                                PrintWriter out = response.getWriter();
                                out.print(responseBody);
                                response.flushBuffer();
                            }
                        } else {
                            // Patch_AGG 20112505 Prevents IE cache
                            if (request.getProtocol().compareTo("HTTP/1.0") == 0) {
                                response.setHeader("Pragma", "no-cache");
                            } else if (request.getProtocol().compareTo("HTTP/1.1") == 0) {
                                response.setHeader("Cache-Control", "no-cache");
                            }
                            response.setDateHeader("Expires", -1);
                            // END patch
                            // Copy request to response
                            InputStream st = httppost.getResponseBodyAsStream();
                            final ServletOutputStream sos = response.getOutputStream();
                            IOUtils.copy(st, sos);
                        }
                    } else {
                        strErrorMessage += errorType;
                        log.error(strErrorMessage);
                    }
                } else if (httppost.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
                    response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                    response.addHeader(WWW_AUTHENTICATE,
                            httppost.getResponseHeader(WWW_AUTHENTICATE).getValue());
                } else {
                    strErrorMessage = "Unexpected failure: ".concat(httppost.getStatusLine().toString())
                            .concat(" ").concat(httppost.getResponseBodyAsString());
                    log.error("Unexpected failure: " + httppost.getStatusLine().toString());
                }
                httppost.releaseConnection();
                // AGG 20110927 Avoid Throwable (change it with exceptions)
            } catch (Exception e) {
                log.error("Error al tratar el contenido de la peticion: " + e.getMessage(), e);
            } finally {
                if (httppost != null) {
                    httppost.releaseConnection();
                }
            }
        } else {
            strErrorMessage += "Only HTTP(S) protocol supported";
            log.error("Only HTTP(S) protocol supported");
            // throw new
            // ServletException("only HTTP(S) protocol supported");
        }
    }
    // There are errors.
    if (!strErrorMessage.equals("") || serverUrl.equals("ERROR")) {
        if (strErrorMessage.equals("") == true) {
            strErrorMessage = "Error en el parametro url de entrada";
        }
        // String errorXML = strErrorMessage;
        String errorXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error><descripcion>" + strErrorMessage
                + "</descripcion></error>";
        response.setContentType("text/xml");
        try {
            PrintWriter out = response.getWriter();
            out.print(errorXML);
            response.flushBuffer();
        } catch (Exception e) {
            log.error(e);
        }
    }
    log.info("-------- End POST method --------");
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Test method header for supported authentication mode,
 * return true if Basic authentication is not available
 *
 * @param getMethod http method/*from   w w w. j  a  v a2s.  co  m*/
 * @return true if only NTLM is enabled
 */
public static boolean acceptsNTLMOnly(HttpMethod getMethod) {
    Header authenticateHeader = null;
    if (getMethod.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        authenticateHeader = getMethod.getResponseHeader("WWW-Authenticate");
    } else if (getMethod.getStatusCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
        authenticateHeader = getMethod.getResponseHeader("Proxy-Authenticate");
    }
    if (authenticateHeader == null) {
        return false;
    } else {
        boolean acceptBasic = false;
        boolean acceptNTLM = false;
        HeaderElement[] headerElements = authenticateHeader.getElements();
        for (HeaderElement headerElement : headerElements) {
            if ("NTLM".equalsIgnoreCase(headerElement.getName())) {
                acceptNTLM = true;
            }
            if ("Basic realm".equalsIgnoreCase(headerElement.getName())) {
                acceptBasic = true;
            }
        }
        return acceptNTLM && !acceptBasic;

    }
}

From source file:com.esri.gpt.framework.http.HttpClientRequest.java

/**
 * Executes the HTTP request./*from  w w  w. j  a  v  a2s  .c  o  m*/
 * @throws IOException if an Exception occurs
 */
public void execute() throws IOException {

    // initialize
    this.executionLog.setLength(0);
    StringBuffer log = this.executionLog;
    ResponseInfo respInfo = this.getResponseInfo();
    respInfo.reset();
    InputStream responseStream = null;
    HttpMethodBase method = null;

    try {
        log.append("HTTP Client Request\n").append(this.getUrl());

        // make the Apache HTTPClient
        HttpClient client = this.batchHttpClient;
        if (client == null) {
            client = new HttpClient();
            boolean alwaysClose = Val.chkBool(Val.chkStr(ApplicationContext.getInstance().getConfiguration()
                    .getCatalogConfiguration().getParameters().getValue("httpClient.alwaysClose")), false);
            if (alwaysClose) {
                client.setHttpConnectionManager(new SimpleHttpConnectionManager(true));
            }
        }

        // setting timeout info
        client.getHttpConnectionManager().getParams().setConnectionTimeout(getConnectionTimeOutMs());
        client.getHttpConnectionManager().getParams().setSoTimeout(getResponseTimeOutMs());

        // setting retries
        int retries = this.getRetries();

        // create the client and method, apply authentication and proxy settings
        method = this.createMethod();
        //method.setFollowRedirects(true);
        if (retries > -1) {
            // TODO: not taking effect yet?
            DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retries, true);
            client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
        }

        this.applyAuthAndProxySettings(client, this.getUrl());

        // execute the method, determine basic information about the response
        respInfo.setResponseCode(client.executeMethod(method));
        this.determineResponseInfo(method);

        // collect logging info
        if (LOGGER.isLoggable(Level.FINER)) {
            log.append("\n>>").append(method.getStatusLine());
            log.append("\n--Request Header");
            for (Header hdr : method.getRequestHeaders()) {
                log.append("\n  ").append(hdr.getName() + ": " + hdr.getValue());
            }
            log.append("\n--Response Header");
            for (Header hdr : method.getResponseHeaders()) {
                log.append("\n  ").append(hdr.getName() + ": " + hdr.getValue());
            }

            //log.append(" responseCode=").append(this.getResponseInfo().getResponseCode());
            //log.append(" responseContentType=").append(this.getResponseInfo().getContentType());
            //log.append(" responseContentEncoding=").append(this.getResponseInfo().getContentEncoding());
            //log.append(" responseContentLength=").append(this.getResponseInfo().getContentLength());

            if (this.getContentProvider() != null) {
                String loggable = this.getContentProvider().getLoggableContent();
                if (loggable != null) {
                    log.append("\n--Request Content------------------------------------\n").append(loggable);
                }
            }
        }

        // throw an exception if an error is encountered
        if ((respInfo.getResponseCode() < 200) || (respInfo.getResponseCode() >= 300)) {
            String msg = "HTTP Request failed: " + method.getStatusLine();
            if (respInfo.getResponseCode() == HttpStatus.SC_UNAUTHORIZED) {
                AuthState authState = method.getHostAuthState();
                AuthScheme authScheme = authState.getAuthScheme();
                HttpClient401Exception authException = new HttpClient401Exception(msg);
                authException.setUrl(this.getUrl());
                authException.setRealm(authState.getRealm());
                authException.setScheme(authScheme.getSchemeName());
                if ((authException.getRealm() == null) || (authException.getRealm().length() == 0)) {
                    authException.setRealm(authException.generateHostBasedRealm());
                }
                throw authException;
            } else {
                throw new HttpClientException(respInfo.getResponseCode(), msg);
            }
        }

        // handle the response
        if (this.getContentHandler() != null) {
            if (getContentHandler().onBeforeReadResponse(this)) {
                responseStream = getResponseStream(method);
                if (responseStream != null) {
                    this.getContentHandler().readResponse(this, responseStream);
                }
            }

            // log thre response content
            String loggable = this.getContentHandler().getLoggableContent();
            long nBytesRead = this.getResponseInfo().getBytesRead();
            long nCharsRead = this.getResponseInfo().getCharactersRead();
            if ((nBytesRead >= 0) || (nCharsRead >= 0) || (loggable != null)) {
                log.append("\n--Response Content------------------------------------");
                if (nBytesRead >= 0)
                    log.append("\n(").append(nBytesRead).append(" bytes read)");
                if (nCharsRead >= 0)
                    log.append("\n(").append(nCharsRead).append(" characters read)");
                if (loggable != null)
                    log.append("\n").append(loggable);
            }
        }

    } finally {

        // cleanup
        try {
            if (responseStream != null)
                responseStream.close();
        } catch (Throwable t) {
            LOGGER.log(Level.SEVERE, "Unable to close HTTP response stream.", t);
        }
        try {
            if (method != null)
                method.releaseConnection();
        } catch (Throwable t) {
            LOGGER.log(Level.SEVERE, "Unable to release HttpMethod", t);
        }

        // log the request/response
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(this.getExecutionLog().toString());
        }
    }
}

From source file:edu.psu.iam.cpr.core.util.Utility.java

/**
 * This method is used to convert an CPR status code to an analogous HTTP status code.
 *
 * @param statusCode contains the CPR status code.
 * @return will return the HTTP status code.
 *//*from   ww  w  .ja v a  2  s. c om*/
public static int convertCprReturnToHttpStatus(final int statusCode) {

    final ReturnType returnType = ReturnType.get(statusCode);
    int httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;

    switch (returnType) {
    case SUCCESS:
        httpStatus = HttpStatus.SC_OK;
        break;
    case ADD_FAILED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case ALREADY_DELETED_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case RECORD_NOT_FOUND_EXCEPTION:
        httpStatus = HttpStatus.SC_NOT_FOUND;
        break;
    case ARCHIVE_FAILED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case NOT_SPECIFIED_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case TYPE_NOT_FOUND_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case INVALID_PARAMETERS_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case YN_PARAMETERS_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case GENERAL_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case PARAMETER_LENGTH_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case MESSAGE_CREATION_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case MESSAGE_INITIALIZATION_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case MESSAGE_SEND_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case NOT_AUTHORIZED_EXCEPTION:
        httpStatus = HttpStatus.SC_UNAUTHORIZED;
        break;
    case PERSON_NOT_ACTIVE_EXCEPTION:
        httpStatus = HttpStatus.SC_BAD_REQUEST;
        break;
    case PERSON_NOT_FOUND_EXCEPTION:
        httpStatus = HttpStatus.SC_NOT_FOUND;
        break;
    case PSUID_NOT_FOUND_EXCEPTION:
        httpStatus = HttpStatus.SC_NOT_FOUND;
        break;
    case SERVICE_AUTHENTICATION_EXCEPTION:
        httpStatus = HttpStatus.SC_UNAUTHORIZED;
        break;
    case SET_PRIMARY_FAILED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case UPDATE_FAILED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case WEB_SERVICE_NOT_FOUND_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case GI_FAILURE:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case DB_CONNECTION_FAILURE:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case GENERAL_DATABASE_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case UNARCHIVE_FAILED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case DATA_CHANGE_EXCEPTION:
        httpStatus = HttpStatus.SC_UNAUTHORIZED;
        break;
    case SECURITY_OPERATION_EXCEPTION:
        httpStatus = HttpStatus.SC_UNAUTHORIZED;
        break;
    case AFFILIATION_USE_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case IAP_USE_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case RECORD_ALREADY_EXISTS:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case EXACT_MATCH_EXCEPTION:
        httpStatus = HttpStatus.SC_MULTIPLE_CHOICES;
        break;
    case NEAR_MATCH_EXCEPTION:
        httpStatus = HttpStatus.SC_MULTIPLE_CHOICES;
        break;
    case MESSAGE_RECEIVE_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case DIRECTORY_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case JSON_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case JMS_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    case NOT_IMPLEMENTED_EXCEPTION:
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        break;
    default:
        break;
    }
    return httpStatus;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Execute Get method, do not follow redirects.
 *
 * @param httpClient      Http client instance
 * @param method          Http method// w w  w .  j a  v  a  2  s .  com
 * @param followRedirects Follow redirects flag
 * @throws IOException on error
 */
public static void executeGetMethod(HttpClient httpClient, GetMethod method, boolean followRedirects)
        throws IOException {
    // do not follow redirects in expired sessions
    method.setFollowRedirects(followRedirects);
    int status = httpClient.executeMethod(method);
    if ((status == HttpStatus.SC_UNAUTHORIZED || status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED)
            && acceptsNTLMOnly(method) && !hasNTLM(httpClient)) {
        resetMethod(method);
        LOGGER.debug("Received " + status + " unauthorized at " + method.getURI() + ", retrying with NTLM");
        addNTLM(httpClient);
        status = httpClient.executeMethod(method);
    }
    if (status != HttpStatus.SC_OK && (followRedirects || !isRedirect(status))) {
        LOGGER.warn("GET failed with status " + status + " at " + method.getURI());
        if (status != HttpStatus.SC_NOT_FOUND && status != HttpStatus.SC_FORBIDDEN) {
            LOGGER.warn(method.getResponseBodyAsString());
        }
        throw DavGatewayHttpClientFacade.buildHttpException(method);
    }
    // check for expired session
    if (followRedirects) {
        String queryString = method.getQueryString();
        checkExpiredSession(queryString);
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

protected void innerProcess(final CrawlURI curi) throws InterruptedException {
    if (!canFetch(curi)) {
        // Cannot fetch this, due to protocol, retries, or other problems
        return;//from   ww w.  ja  v  a2  s . com
    }

    this.curisHandled++;

    // Note begin time
    curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());

    // Get a reference to the HttpRecorder that is set into this ToeThread.
    HttpRecorder rec = HttpRecorder.getHttpRecorder();

    // Shall we get a digest on the content downloaded?
    boolean digestContent = ((Boolean) getUncheckedAttribute(curi, ATTR_DIGEST_CONTENT)).booleanValue();
    String algorithm = null;
    if (digestContent) {
        algorithm = ((String) getUncheckedAttribute(curi, ATTR_DIGEST_ALGORITHM));
        rec.getRecordedInput().setDigest(algorithm);
    } else {
        // clear
        rec.getRecordedInput().setDigest((MessageDigest) null);
    }

    // Below we do two inner classes that add check of midfetch
    // filters just as we're about to receive the response body.
    String curiString = curi.getUURI().toString();
    HttpMethodBase method = null;
    if (curi.isPost()) {
        method = new HttpRecorderPostMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    } else {
        method = new HttpRecorderGetMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    }

    HostConfiguration customConfigOrNull = configureMethod(curi, method);

    // Set httpRecorder into curi. Subsequent code both here and later
    // in extractors expects to find the HttpRecorder in the CrawlURI.
    curi.setHttpRecorder(rec);

    // Populate credentials. Set config so auth. is not automatic.
    boolean addedCredentials = populateCredentials(curi, method);
    method.setDoAuthentication(addedCredentials);

    // set hardMax on bytes (if set by operator)
    long hardMax = getMaxLength(curi);
    // set overall timeout (if set by operator)
    long timeoutMs = 1000 * getTimeout(curi);
    // Get max fetch rate (bytes/ms). It comes in in KB/sec
    long maxRateKBps = getMaxFetchRate(curi);
    rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps);

    try {
        this.http.executeMethod(customConfigOrNull, method);
    } catch (RecorderTooMuchHeaderException ex) {
        // when too much header material, abort like other truncations
        doAbort(curi, method, HEADER_TRUNC);
    } catch (IOException e) {
        failedExecuteCleanup(method, curi, e);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions in native
        // code... see
        // http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        failedExecuteCleanup(method, curi, e);
        return;
    }

    // set softMax on bytes to get (if implied by content-length) 
    long softMax = method.getResponseContentLength();

    try {
        if (!method.isAborted()) {
            // Force read-to-end, so that any socket hangs occur here,
            // not in later modules.
            rec.getRecordedInput().readFullyOrUntil(softMax);
        }
    } catch (RecorderTimeoutException ex) {
        doAbort(curi, method, TIMER_TRUNC);
    } catch (RecorderLengthExceededException ex) {
        doAbort(curi, method, LENGTH_TRUNC);
    } catch (IOException e) {
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions from native code
        // see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } finally {
        // ensure recording has stopped
        rec.closeRecorders();
        if (!method.isAborted()) {
            method.releaseConnection();
        }
        // Note completion time
        curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());
        // Set the response charset into the HttpRecord if available.
        setCharacterEncoding(rec, method);
        setSizes(curi, rec);
    }

    if (digestContent) {
        curi.setContentDigest(algorithm, rec.getRecordedInput().getDigestValue());
    }
    if (logger.isLoggable(Level.INFO)) {
        logger.info((curi.isPost() ? "POST" : "GET") + " " + curi.getUURI().toString() + " "
                + method.getStatusCode() + " " + rec.getRecordedInput().getSize() + " "
                + curi.getContentType());
    }

    if (curi.isSuccess() && addedCredentials) {
        // Promote the credentials from the CrawlURI to the CrawlServer
        // so they are available for all subsequent CrawlURIs on this
        // server.
        promoteCredentials(curi);
        if (logger.isLoggable(Level.FINE)) {
            // Print out the cookie.  Might help with the debugging.
            Header setCookie = method.getResponseHeader("set-cookie");
            if (setCookie != null) {
                logger.fine(setCookie.toString().trim());
            }
        }
    } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        // 401 is not 'success'.
        handle401(method, curi);
    }

    if (rec.getRecordedInput().isOpen()) {
        logger.severe(curi.toString() + " RIS still open. Should have" + " been closed by method release: "
                + Thread.currentThread().getName());
        try {
            rec.getRecordedInput().close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "second-chance RIS close failed", e);
        }
    }
}

From source file:com.cyberway.issue.crawler.datamodel.CrawlURI.java

/**
 * Ask this URI if it was a success or not.
 *
 * Only makes sense to call this method after execution of
 * HttpMethod#execute. Regard any status larger then 0 as success
 * except for below caveat regarding 401s.  Use {@link #is2XXSuccess()} if
 * looking for a status code in the 200 range.
 *
 * <p>401s caveat: If any rfc2617 credential data present and we got a 401
 * assume it got loaded in FetchHTTP on expectation that we're to go around
 * the processing chain again. Report this condition as a failure so we
 * get another crack at the processing chain only this time we'll be making
 * use of the loaded credential data./*  www. j av  a 2  s .  co  m*/
 *
 * @return True if ths URI has been successfully processed.
 * @see #is2XXSuccess()
 */
public boolean isSuccess() {
    boolean result = false;
    int statusCode = this.fetchStatus;
    if (statusCode == HttpStatus.SC_UNAUTHORIZED && hasRfc2617CredentialAvatar()) {
        result = false;
    } else {
        result = (statusCode > 0);
    }
    return result;
}

From source file:com.thoughtworks.go.server.service.ValueStreamMapServiceTest.java

@Test
public void shouldPopulateErrorWhenUserDoesNotHaveViewPermissionForCurrentPipeline() throws Exception {
    /*// w  w  w. jav a 2 s  .  c  o m
        git --> p1
     */

    GitMaterial git = new GitMaterial("git");
    String pipelineName = "p1";
    CruiseConfig cruiseConfig = new BasicCruiseConfig(new BasicPipelineConfigs(
            PipelineConfigMother.pipelineConfig("p1", new MaterialConfigs(git.config()))));
    when(goConfigService.currentCruiseConfig()).thenReturn(cruiseConfig);

    BuildCause p1buildCause = createBuildCause(new ArrayList<String>(), asList(git));
    when(pipelineService.buildCauseFor("p1", 1)).thenReturn(p1buildCause);
    when(pipelineService.findPipelineByCounterOrLabel(pipelineName, "1"))
            .thenReturn(new Pipeline("p1", "label-1", p1buildCause));

    Username newUser = new Username(new CaseInsensitiveString("looser"));
    when(securityService.hasViewPermissionForPipeline(newUser, pipelineName)).thenReturn(false);

    valueStreamMapService.getValueStreamMap(pipelineName, 1, newUser, result);

    assertResult(HttpStatus.SC_UNAUTHORIZED, "PIPELINE_CANNOT_VIEW");
}

From source file:com.cyberway.issue.crawler.frontier.AdaptiveRevisitFrontier.java

/**
 * Checks if a recently completed CrawlURI that did not finish successfully
 * needs to be retried immediately (processed again as soon as politeness
 * allows.)/*w  w  w.  j  a v  a 2  s.  co  m*/
 *
 * @param curi The CrawlURI to check
 * @return True if we need to retry promptly.
 * @throws AttributeNotFoundException If problems occur trying to read the
 *            maximum number of retries from the settings framework.
 */
protected boolean needsPromptRetry(CrawlURI curi) throws AttributeNotFoundException {
    if (curi.getFetchAttempts() >= ((Integer) getAttribute(ATTR_MAX_RETRIES, curi)).intValue()) {
        return false;
    }

    switch (curi.getFetchStatus()) {
    case S_DEFERRED:
        return true;

    case HttpStatus.SC_UNAUTHORIZED:
        // We can get here though usually a positive status code is
        // a success.  We get here if there is rfc2617 credential data
        // loaded and we're supposed to go around again.  See if any
        // rfc2617 credential present and if there, assume it got
        // loaded in FetchHTTP on expectation that we're to go around
        // again.  If no rfc2617 loaded, we should not be here.
        boolean loaded = curi.hasRfc2617CredentialAvatar();
        if (!loaded) {
            logger.severe("Have 401 but no creds loaded " + curi);
        }
        return loaded;

    default:
        return false;
    }
}