Example usage for org.apache.commons.collections MultiMap keySet

List of usage examples for org.apache.commons.collections MultiMap keySet

Introduction

In this page you can find the example usage for org.apache.commons.collections MultiMap keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a Set view of the keys contained in this map.

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();/*from   w  ww  .  j a  v  a 2 s.  co  m*/
    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);
    }
}

From source file:org.openconcerto.sql.element.SQLElement.java

@SuppressWarnings("rawtypes")
private final String toString(MultiMap descs) {
    final List<String> l = new ArrayList<String>();
    final Iterator iter = descs.keySet().iterator();
    while (iter.hasNext()) {
        final SQLTable t = (SQLTable) iter.next();
        final Collection rows = (Collection) descs.get(t);
        final SQLElement elem = getElement(t);
        l.add(elemToString(rows.size(), elem));
    }/*from w w  w . j a v a2 s.  c  om*/
    return CollectionUtils.join(l, "\n");
}

From source file:org.yes.cart.service.domain.impl.PriceServiceImpl.java

/**
 * Atm we can have different price definitions (lowest in list with high priority):
 * price without any time limitations;//from ww w.  j a  va  2 s. c o  m
 * price, which starts in infinitive past and will be end at some date;
 * price, which has the start date but no end date;
 * price with start and end date.
 *
 * @param skuPrices all prices filtered by currency, and quantity for all skus
 * @return the list of sku prices, which is filtered by time frame
 */
List<Pair<String, SkuPrice>> getSkuPricesFilteredByTimeFrame(final List<Pair<String, SkuPrice>> skuPrices) {

    final List<Pair<String, SkuPrice>> allPrices = new LinkedList<Pair<String, SkuPrice>>();

    final MultiMap qtySkuPriceMap = new MultiValueMap();

    for (Pair<String, SkuPrice> skuPrice : skuPrices) {
        qtySkuPriceMap.put(skuPrice.getFirst() + ":" + skuPrice.getSecond().getQuantity(), skuPrice);
    }

    for (Object o : qtySkuPriceMap.keySet()) {

        final String key = (String) o;

        final List<Pair<String, SkuPrice>> skuPricesForOneSku = new ArrayList<Pair<String, SkuPrice>>(
                (Collection<Pair<String, SkuPrice>>) qtySkuPriceMap.get(key));

        reorderSkuPrices(skuPricesForOneSku);

        long time = java.lang.System.currentTimeMillis(); //TODO: V2 time machine

        boolean found = false;

        found = addFramedPrice(allPrices, skuPricesForOneSku, time);

        if (!found) {
            found = addEndPrice(allPrices, skuPricesForOneSku, time);
        }

        if (!found) {
            found = addStartPrice(allPrices, skuPricesForOneSku, time);
        }

        if (!found) {
            addAllTimePrice(allPrices, skuPricesForOneSku, time);
        }

    }

    return allPrices;
}