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:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

protected void innerProcess(final CrawlURI curi) throws InterruptedException {
    if (!canFetch(curi)) {
        // Cannot fetch this, due to protocol, retries, or other problems
        return;//from   w w  w .  j ava 2s  .  c  o m
    }

    HttpClient http = this.getClient();
    setLocalIP(http);

    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 {
        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 (!curi.isSeed() && curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED) {
            logger.debug(curi.getUURI().toString() + " is not modify");
            curi.skipToProcessorChain(getController().getPostprocessorChain());
        } else 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();
        logger.debug("cloase backup file.&uri= " + curi.getCrawlURIString());
        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());
    }

    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.isDebugEnabled()) {
            // Print out the cookie.  Might help with the debugging.
            Header setCookie = method.getResponseHeader("set-cookie");
            if (setCookie != null) {
                logger.debug(setCookie.toString().trim());
            }
        }
    } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        // 401 is not 'success'.
        handle401(method, curi);
    }

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

From source file:org.archive.crawler.frontier.AbstractFrontier.java

/**
 * Checks if a recently() processed CrawlURI that did not finish successfully
 * needs to be reenqueued (and thus possibly, processed again after some 
 * time elapses)//  www.ja v  a2  s  .c om
 * 
 * <br><br>
 * 
 * ?? CrawlURI ???
 * 
 * @param curi
 *            The CrawlURI to check
 * @return True if we need to retry.
 */
protected boolean needsReenqueuing(CrawlURI curi) {
    if (overMaxRetries(curi)) {
        return false;
    }

    switch (curi.getFetchStatus()) {
    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.hasRfc2617Credential();
        if (!loaded && logger.isLoggable(Level.FINE)) {
            logger.fine("Have 401 but no creds loaded " + curi);
        }
        return loaded;
    case S_DEFERRED:
    case S_CONNECT_FAILED:
    case S_CONNECT_LOST:
    case S_DOMAIN_UNRESOLVABLE:
        // these are all worth a retry
        // TODO: consider if any others (S_TIMEOUT in some cases?) deserve
        // retry
        return true;
    case S_UNATTEMPTED:
        if (curi.includesRetireDirective()) {
            return true;
        } // otherwise, fall-through: no status is an error without queue-directive
    default:
        return false;
    }
}

From source file:org.archive.modules.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.// w w w . j a v  a2s . 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 && hasRfc2617Credential()) {
        result = false;
    } else {
        result = (statusCode > 0);
    }
    return result;
}

From source file:org.archive.modules.Processor.java

public static boolean isSuccess(CrawlURI puri) {
    boolean result = false;
    int statusCode = puri.getFetchStatus();
    if (statusCode == HttpStatus.SC_UNAUTHORIZED && hasHttpAuthenticationCredential(puri)) {
        result = false;//from  w  w w. j  a  v a2s .  c o m
    } else {
        result = (statusCode > 0);
    }
    return result;
}

From source file:org.artifactory.rest.common.ArtifactoryRestExceptionMapper.java

@Override
public Response toResponse(WebApplicationException exception) {
    Response jerseyResponse = exception.getResponse();
    int status = jerseyResponse.getStatus();
    switch (status) {
    case HttpStatus.SC_FORBIDDEN:
        return interceptForbiddenStatus(jerseyResponse);

    case HttpStatus.SC_UNAUTHORIZED:
        return interceptUnauthorizedStatus(jerseyResponse);
    }//w w  w  .j  a  v a2  s .  c o m

    return jerseyResponse;
}

From source file:org.artifactory.rest.common.ArtifactoryRestExceptionMapper.java

private Response createUnauthorizedResponseWithChallenge(Response jerseyResponse) {
    return Response.status(HttpStatus.SC_UNAUTHORIZED).entity(jerseyResponse.getEntity())
            .header("WWW-Authenticate", "Basic realm=\"" + authenticationEntryPoint.getRealmName() + "\"")
            .build();//from ww w  .  j a  v a2  s.co m
}

From source file:org.artifactory.webapp.servlet.HttpArtifactoryResponse.java

@Override
public void sendAuthorizationRequired(String message, String realm) throws IOException {
    try {//from w w w .j a v a2  s. c o m
        response.addHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
        response.sendError(HttpStatus.SC_UNAUTHORIZED, message);
        flush();
    } catch (IOException e) {
        throw e;
    } catch (IllegalStateException e) {
        log.warn("Failed to send http error (" + e.getMessage() + ").", e);
    } catch (Throwable t) {
        log.warn("Failed to send http error (" + t.getMessage() + ").", t);
    }
}

From source file:org.buddycode.jaxrs.samplejaxrs.authentication.AuthenticationFilter.java

private Response handleError() {
    log.error("Failed authentication");
    return Response.status(HttpStatus.SC_UNAUTHORIZED).entity(new StandardResponse("failed authentication"))
            .build();/* w w w. j  a va  2  s  .c o m*/
}

From source file:org.collectionspace.chain.csp.webui.misc.WebReset.java

private boolean initialiseAll(Storage storage, UIRequest request, String path, boolean modifyResponse)
        throws UIException {
    StringBuffer responseMessage = new StringBuffer();
    boolean initializationFailed = false;
    boolean initializationUnknown = false;

    try {//from w  ww .j  a va 2s.com
        logInitMessage(responseMessage, "Initializing vocab/auth entries...", modifyResponse);
        JSONObject myjs = new JSONObject();
        myjs.put("pageSize", "10");
        myjs.put("pageNum", "0");
        JSONObject data = storage.getPathsJSON("/", null);
        String[] paths = (String[]) data.get("listItems");
        for (String dir : paths) {
            try {
                if (this.spec.hasRecord(dir)) {
                    Record record = this.spec.getRecord(dir);
                    if (record.isType("authority") == true) {
                        for (Instance instance : record.getAllInstances()) {
                            if (instance.getCreateUnreferenced() || isInstanceReferenced(instance)) {
                                avi = new AuthoritiesVocabulariesInitialize(instance, populate, modifyResponse);
                                Option[] allOpts = instance.getAllOptions();
                                boolean creatingTerm = false;
                                try {
                                    if (avi.createIfMissingAuthority(storage, responseMessage, record,
                                            instance) == -1) {
                                        log.warn(String.format(
                                                "The currently authenticated user does not have sufficient permission to determine if the '%s' authority/term-list is properly initialized.",
                                                instance.getID()));
                                        initializationUnknown = true; // since the logged in user doesn't have the correct perms, we can't verify that the authorities and term lists have been properly initialized
                                    } else {
                                        //
                                        // Create the missing items.
                                        //
                                        creatingTerm = true;
                                        avi.fillVocab(storage, record, instance, responseMessage, allOpts,
                                                true);
                                    }
                                } catch (UnderlyingStorageException e) {
                                    if (e.getStatus() == HttpStatus.SC_CONFLICT) {
                                        // This means the authority/vocabulary instance already exists in the backend, so move on to the next instance.
                                        log.warn(String.format(
                                                "A short ID for the authority/vocabulary instance '%s' already exists.",
                                                instance.getID()));
                                        continue; // Not a fatal error.
                                    } else {
                                        throw e;
                                    }
                                } catch (Exception e) {
                                    if (avi.success() == false) {
                                        initializationFailed = true;
                                    }
                                    throw e;
                                }
                            } else {
                                logInitMessage(responseMessage,
                                        "Instance " + instance.getID() + " is defined by not referenced.",
                                        modifyResponse);
                            }
                        }
                    }
                }
            } catch (UnderlyingStorageException e) {
                //
                // If we get here, the system is either in an unknown or incomplete initialization state.  If it's incomplete, we'll put up
                // a message.
                //
                if (e.getCause() instanceof ConnectionException) {
                    if (initializationFailed == true) {
                        modifyResponse = true;
                        if (e.getStatus() == HttpStatus.SC_UNAUTHORIZED
                                || e.getStatus() == HttpStatus.SC_FORBIDDEN) {
                            logInitMessage(responseMessage,
                                    "\nSummary:\n\t*** ERROR *** CollectionSpace has not been properly initialized: The CollectionSpace administrator needs to login to the correct tenant and initialize the default term lists and authorities.\n\n",
                                    modifyResponse);
                        } else {
                            logInitMessage(responseMessage,
                                    "\nSummary:\n\t*** ERROR *** CollectionSpace has not been properly initialized: Ask the CollectionSpace administrator to login to the correct tenant and initialize the default term lists and authorities.\n\n",
                                    modifyResponse);
                        }
                    } else if (initializationUnknown == true) {
                        log.warn(
                                "The currently logged in user does not have the correct permissions to determin whether or not the default authorities and term lists have been properly initialized.");
                    } else {
                        throw e; // Should never get here unless we've got a bug in our code
                    }
                }

                logException(e, responseMessage, modifyResponse);
                break; // no need to continue if the user hasn't authenticated or has incorrect permissions
            }
        }
    } catch (ExistException e) {
        logInitMessage(responseMessage, "ExistException " + e.getLocalizedMessage(), modifyResponse);
        throw new UIException("Existence problem", e);
    } catch (UnimplementedException e) {
        logInitMessage(responseMessage, "UnimplementedException " + e.getLocalizedMessage(), modifyResponse);
        throw new UIException("Unimplemented ", e);
    } catch (UnderlyingStorageException x) {
        if (x.getStatus() == HttpStatus.SC_UNAUTHORIZED) {
            initializationFailed = true;
            logInitMessage(responseMessage,
                    "\n*** ERROR *** You need to be logged in to the correct tenant with the proper credentials before attempting to initialize the default term lists and authorities.\n",
                    modifyResponse);
            logException(x, responseMessage, modifyResponse);
        } else {
            logInitMessage(responseMessage, "UnderlyingStorageException " + x.getLocalizedMessage(),
                    modifyResponse);
            throw new UIException("Problem storing:" + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
        }
    } catch (JSONException e) {
        logInitMessage(responseMessage, "JSONException " + e.getLocalizedMessage(), modifyResponse);
        throw new UIException("Invalid JSON", e);
    }

    //
    // If the caller is requesting we add our messages to the HTTP request response, then create a
    // TTY out instance and add our messages.
    //
    if (modifyResponse == true && request != null) {
        TTYOutputter tty = request.getTTYOutputter();
        tty.line(responseMessage.toString());
    }

    return !initializationFailed; // report success if we didn't see a failure
}

From source file:org.collectionspace.chain.csp.webui.nuispec.UISchema.java

private void assertLoginStatus(UISession uiSession) throws UnauthorizedException {
    if (uiSession == null || uiSession.getValue(UISession.USERID) == null) {
        throw new UnauthorizedException("User must be authenticated to access this resource.",
                HttpStatus.SC_UNAUTHORIZED, "/namespaces");
    }/*from  w  w  w  . j  ava 2 s.  com*/
}