Example usage for org.apache.commons.collections SetUtils typedSet

List of usage examples for org.apache.commons.collections SetUtils typedSet

Introduction

In this page you can find the example usage for org.apache.commons.collections SetUtils typedSet.

Prototype

public static Set typedSet(Set set, Class type) 

Source Link

Document

Returns a typed set backed by the given set.

Usage

From source file:org.lockss.proxy.ProxyHandler.java

public void handle0(String pathInContext, String pathParams, HttpRequest request, HttpResponse response)
        throws HttpException, IOException {
    URI uri = request.getURI();//  w  w  w.ja va  2 s  .  com
    long reqStartTime = TimeBase.nowMs();
    if (proxyMgr.isLogReqStart()) {
        log.info("Proxy handle url: " + uri);
    }

    if (!proxyMgr.isMethodAllowed(request.getMethod())) {
        sendForbid(request, response, uri);
        logAccess(request, "forbidden method: " + request.getMethod());
        return;
    }

    // Is this a CONNECT request?
    if (HttpRequest.__CONNECT.equals(request.getMethod())) {
        response.setField(HttpFields.__Connection, "close"); // XXX Needed for IE????
        handleConnect(pathInContext, pathParams, request, response);
        return;
    }

    // The URI in https proxy requests is generally host-relative.  Detect
    // this and replace the URI in the request with the absolute URI.  Do
    // this only for SSL connections, else accessing directly with browser
    // can cause a loop.
    HttpConnection conn = request.getHttpConnection();
    if (sslListenPort > 0 && sslListenPort == conn.getServerPort()) {
        if (uri.toString().startsWith("/")) {
            String root = request.getRootURL().toString();
            if (!StringUtil.isNullString(root)) {
                String newUri = root.toLowerCase() + uri.toString();
                log.debug("Relative URI: " + uri + " -> " + newUri);
                uri.setURI(newUri);
            }
        }
    }

    if (log.isDebug3()) {
        log.debug3("pathInContext=" + pathInContext);
        log.debug3("URI=" + uri);
    }

    // Handling post requests specially.
    // During a crawl, we can store links from a POST form similar to a GET form.
    // See TestHtmlParserLinkExtractor::testPOSTForm.
    //
    // This logic is similar to logic in ServeContent.
    if (HttpRequest.__POST.equals(request.getMethod()) && handleFormPost) {
        log.debug3("POST request found!");
        // We don't have any way to know order from request headers instead to
        // so we just sort the elements.
        MultiMap params = request.getParameters();
        Set<String> unsortedKeys = SetUtils.typedSet(params.keySet(), String.class);
        SortedSet<String> keys = new TreeSet<String>(unsortedKeys);

        FormUrlHelper helper = new FormUrlHelper(uri.toString());

        for (String k : keys) {
            helper.add((k), params.get(k).toString());
        }
        if (log.isDebug3()) {
            log.debug3("Overriding original URI to:" + helper.toEncodedString());
        }
        URI postUri = new URI(helper.toEncodedString());
        // We only want to override the post request by proxy if we cached it during crawling.
        CachedUrl cu = pluginMgr.findCachedUrl(postUri.toString());
        if (cu != null) {
            uri = postUri;
        }
    }

    if (isFailOver) {
        if (uri.getHost() == null && failOverTargetUri.getHost() != null) {
            uri.setHost(failOverTargetUri.getHost());
            uri.setPort(failOverTargetUri.getPort());
            uri.setScheme(failOverTargetUri.getScheme());
        }
        if (log.isDebug2())
            log.debug2("Failover URI: " + uri);
    } else {
        // XXX what to do here?
    }

    String urlString = uri.toString();
    if (MANIFEST_INDEX_URL_PATH.equals(urlString)) {
        sendIndexPage(request, response);
        logAccess(request, "200 index page", TimeBase.msSince(reqStartTime));
        return;
    }
    String unencUrl = urlString;
    if (proxyMgr.isMinimallyEncodeUrls()) {
        urlString = UrlUtil.minimallyEncodeUrl(urlString);
        if (!urlString.equals(unencUrl)) {
            log.debug("Encoded " + unencUrl + " to " + urlString);
        }
    }

    // Does the URL point to a resolver rather than a
    // server?
    /* PJG: DOIs now resolved by ServeContent
        String resolvedUrl = MetadataUtil.proxyResolver(urlString);
        if (resolvedUrl != null) {
          // Yes - send a redirect
          sendRedirect(request, response, resolvedUrl);
          logAccess(request, "302 redirect to resolved DOI");
          return;
        }
    */
    CachedUrl cu = pluginMgr.findCachedUrl(urlString);

    // Don't allow CLOCKSS to serve local content for unsubscribed AUs
    if (cu != null && theDaemon.isDetectClockssSubscription() && !auditProxy) {
        ArchivalUnit au = cu.getArchivalUnit();
        switch (AuUtil.getAuState(au).getClockssSubscriptionStatus()) {
        case AuState.CLOCKSS_SUB_UNKNOWN:
        case AuState.CLOCKSS_SUB_NO:
        case AuState.CLOCKSS_SUB_INACCESSIBLE:
            // If CLOCKSS unsubscribed content, forget that we have local copy
            cu = null;
            break;
        case AuState.CLOCKSS_SUB_YES:
            break;
        }
    }

    try {
        boolean isRepairRequest = proxyMgr.isRepairRequest(request);
        boolean isInCache = cu != null && cu.hasContent();

        if (log.isDebug2()) {
            log.debug2("cu: " + (isRepairRequest ? "(repair) " : "") + cu);
        }
        String source = request.getField(Constants.X_LOCKSS_SOURCE);

        if (isRepairRequest || neverProxy || Constants.X_LOCKSS_SOURCE_CACHE.equals(source)
                || (isInCache && proxyMgr.isHostDown(uri.getHost()))) {
            if (isInCache) {
                if (isRepairRequest && log.isDebug()) {
                    log.debug("Serving repair to " + request.getRemoteAddr() + ", " + cu);
                }
                serveFromCache(pathInContext, pathParams, request, response, cu);
                logAccess(request, "200 from cache", TimeBase.msSince(reqStartTime));
                // Record the necessary information required for COUNTER reports.
                CounterReportsRequestRecorder.getInstance().recordRequest(urlString,
                        CounterReportsRequestRecorder.PublisherContacted.FALSE, 200);
                return;
            } else {
                // Not found on cache and told not to forward request
                String errmsg = auditProxy ? "Not found in LOCKSS box " + PlatformUtil.getLocalHostname()
                        : "Not found";
                if (audit503UntilAusStarted && !theDaemon.areAusStarted()) {
                    // TODO - Guesstimate remaining time and add Retry-After header
                    errmsg = "This LOCKSS box is starting.  Please try again in a moment.";
                    response.sendError(HttpResponse.__503_Service_Unavailable, errmsg);
                    request.setHandled(true);
                    logAccess(request, "not present, no forward, 503", TimeBase.msSince(reqStartTime));
                } else if (auditIndex) {
                    sendErrorPage(request, response, 404, errmsg, pluginMgr.getCandidateAus(urlString));
                    logAccess(request, "not present, no forward, 404 w/index", TimeBase.msSince(reqStartTime));
                } else {
                    response.sendError(HttpResponse.__404_Not_Found, errmsg);
                    request.setHandled(true);
                    logAccess(request, "not present, no forward, 404", TimeBase.msSince(reqStartTime));
                }
                return;
            }
        }

        if (!isInCache && !Constants.X_LOCKSS_SOURCE_PUBLISHER.equals(source)
                && (proxyMgr.getHostDownAction() == ProxyManager.HOST_DOWN_NO_CACHE_ACTION_504)
                && proxyMgr.isHostDown(uri.getHost())) {
            sendErrorPage(request, response, 504,
                    hostMsg("Can't connect to", uri.getHost(), "Host not responding (cached status)"),
                    pluginMgr.getCandidateAus(urlString));
            logAccess(request, "not present, host down, 504", TimeBase.msSince(reqStartTime));
            return;
        }
        if (UrlUtil.isHttpUrl(urlString)) {
            if (HttpRequest.__GET.equals(request.getMethod())) {
                doLockss(pathInContext, pathParams, request, response, urlString, cu, reqStartTime);
                return;
            }
        }
        doSun(pathInContext, pathParams, request, response);
        logAccess(request, "unrecognized request type, forwarded", TimeBase.msSince(reqStartTime));
    } finally {
        AuUtil.safeRelease(cu);
    }
}