Example usage for java.util SortedMap entrySet

List of usage examples for java.util SortedMap entrySet

Introduction

In this page you can find the example usage for java.util SortedMap entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

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

Usage

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.writer.SVMHMMDataWriter.java

@Override
public void write(File aOutputDirectory, FeatureStore featureStore, boolean aUseDenseInstances,
        String aLearningMode, boolean applyWeighting) throws Exception {
    // map features to feature numbers
    BidiMap featureNameToFeatureNumberMapping = SVMHMMUtils
            .mapVocabularyToIntegers(featureStore.getFeatureNames());

    // prepare output file
    File outputFile = new File(aOutputDirectory, new SVMHMMAdapter()
            .getFrameworkFilename(TCMachineLearningAdapter.AdapterNameEntries.featureVectorsFile));

    BufferedWriter bf = new BufferedWriter(new FileWriter(outputFile));
    PrintWriter pw = new PrintWriter(bf);

    log.info("Start writing features to file " + outputFile.getAbsolutePath());

    log.debug("Total instances: " + featureStore.getNumberOfInstances());
    log.debug("Feature vector size: " + featureStore.getFeatureNames().size());

    if (featureStore instanceof SparseFeatureStore) {
        SparseFeatureStore sparseFeatureStore = (SparseFeatureStore) featureStore;
        log.debug("Non-null feature sparsity ratio: " + sparseFeatureStore.getFeatureSparsityRatio());
    }//w  w  w  .  j a v  a  2 s.  c om

    for (int i = 0; i < featureStore.getNumberOfInstances(); i++) {
        Instance instance;

        // getInstance() is time-consuming for dense features
        // check whether can we use only sparse features
        if (featureStore instanceof SparseFeatureStore) {
            instance = ((SparseFeatureStore) featureStore).getInstanceSparseFeatures(i);
        } else {
            instance = featureStore.getInstance(i);
        }

        // placeholder for original token
        String originalToken = null;

        // other "features" - meta data features that will be stored in the comment
        SortedMap<String, String> metaDataFeatures = new TreeMap<>();

        // feature values
        SortedMap<Integer, Number> featureValues = new TreeMap<>();
        for (Feature f : instance.getFeatures()) {
            String featureName = f.getName();
            Object featureValue = f.getValue();

            // we ignore null feature values
            if (featureValue == null) {
                continue;
            }

            // get original token stored in OriginalToken feature
            if (OriginalTextHolderFeatureExtractor.ORIGINAL_TEXT.equals(featureName)) {
                // if original token/text was multi line, join it to a single line
                //                    originalToken = ((String) featureValue).replaceAll("\\n", " ");
                originalToken = (String) featureValue;
                continue;
            }

            // handle other possible features as metadata?
            if (isMetaDataFeature(featureName)) {
                metaDataFeatures.put(featureName, (String) featureValue);
                continue;
            }

            // not allow other non-number features
            if (!(featureValue instanceof Number)) {
                log.debug("Only features with number values are allowed, but was " + f);
                continue;
            }

            // in case the feature store produced dense feature vector with zeros for
            // non-present features, we ignore zero value features here
            Number featureValueNumber = (Number) featureValue;
            if (Math.abs(featureValueNumber.doubleValue() - 0d) < EPS) {
                continue;
            }

            // get number and int value of the feature
            Integer featureNumber = (Integer) featureNameToFeatureNumberMapping.get(featureName);

            featureValues.put(featureNumber, featureValueNumber);
        }

        // print formatted output: label name and sequence id
        pw.printf(Locale.ENGLISH, "%s qid:%d ", instance.getOutcome(), instance.getSequenceId());

        // print sorted features
        for (Map.Entry<Integer, Number> entry : featureValues.entrySet()) {
            if (entry.getValue() instanceof Double) {
                // format double on 8 decimal places
                pw.printf(Locale.ENGLISH, "%d:%.8f ", entry.getKey(), entry.getValue().doubleValue());
            } else {
                // format as integer
                pw.printf(Locale.ENGLISH, "%d:%d ", entry.getKey(), entry.getValue().intValue());
            }
        }

        // print original token and label as comment
        pw.printf(Locale.ENGLISH, "# %s %d %s ", instance.getOutcome(), instance.getSequenceId(),
                (originalToken != null) ? (URLEncoder.encode(originalToken, "utf-8")) : "");

        // print meta-data features at the end
        for (Map.Entry<String, String> entry : metaDataFeatures.entrySet()) {
            pw.printf(" %s:%s", URLEncoder.encode(entry.getKey(), "utf-8"),
                    URLEncoder.encode(entry.getValue(), "utf-8"));
        }
        // new line at the end
        pw.println();
    }

    IOUtils.closeQuietly(pw);

    // writing feature mapping
    File mappingFile = new File(aOutputDirectory, "featuresIntToNames_forDebug.txt");
    SVMHMMUtils.saveMappingTextFormat(featureNameToFeatureNumberMapping, mappingFile);

    log.info("Finished writing features to file " + outputFile.getAbsolutePath());
}

From source file:com.google.wave.splash.text.ContentRenderer.java

/**
 * Takes content and applies style and formatting to it based on its
 * annotations and elements.//  w w  w.  ja va 2 s  .co m
 *
 * @param content the content
 * @param annotations the annotations
 * @param elements the elements
 * @param contributors the contributors
 * @return the string
 */
public String renderHtml(final String content, final Annotations annotations,
        final SortedMap<Integer, Element> elements, final List<String> contributors) {
    final StringBuilder builder = new StringBuilder();

    // NOTE(dhanji): This step is enormously important!
    final char[] raw = content.toCharArray();

    final SortedSet<Marker> markers = new TreeSet<Marker>();

    // First add annotations sorted by range.
    for (final Annotation annotation : annotations.asList()) {
        // Ignore anything but style or title annotations.
        final String annotationName = annotation.getName();
        if (annotationName.startsWith("style")) {
            markers.add(Marker.fromAnnotation(annotation, annotation.getRange().getStart(), false));
            markers.add(Marker.fromAnnotation(annotation, annotation.getRange().getEnd(), true));
        } else if (annotationName.startsWith("link")) {
            markers.add(Marker.fromAnnotation(annotation, annotation.getRange().getStart(), false));
            markers.add(Marker.fromAnnotation(annotation, annotation.getRange().getEnd(), true));
        } else if ("conv/title".equals(annotationName)) {
            // Find the first newline and make sure the annotation only gets to that
            // point.
            final int start = annotation.getRange().getStart();
            final int from = raw[0] == '\n' ? 1 : 0;
            final int end = content.indexOf('\n', from);
            if (end > start && start < end) {
                // Commented (vjrj)
                // final Annotation title = new Annotation(Annotation.FONT_WEIGHT,
                // "bold", start, end);
                // markers.add(Marker.fromAnnotation(title, start, false));
                // markers.add(Marker.fromAnnotation(title, end, true));
            } else {
                // LOG?
            }
        }
    }

    // Now add elements sorted by index.
    for (final Map.Entry<Integer, Element> entry : elements.entrySet()) {
        markers.add(Marker.fromElement(entry.getValue(), entry.getKey()));
    }

    builder.append("<p>");
    int cursor = 0;
    for (final Marker marker : markers) {
        if (marker.index > cursor) {
            final int to = Math.min(raw.length, marker.index);
            builder.append(Markup.sanitize(new String(raw, cursor, to - cursor)));
        }

        cursor = marker.index;

        if (marker.isElement()) {
            renderElement(marker.element, marker.index, contributors, builder);
        } else {
            emitAnnotation(marker, builder);
        }
    }

    // add any tail bits
    if (cursor < raw.length - 1) {
        builder.append(Markup.sanitize(new String(raw, cursor, raw.length - cursor)));
    }

    // Replace empty paragraphs. (TODO expensive and silly)
    return builder.append("</ul>").toString().replaceAll("<p>\n</p>", "<p><br/></p>")
            .replaceAll("<p>\n<div style=\"font-size", "<p><br/><div style=\"font-size");
}

From source file:com.thinkthinkdo.pff_appsettings.PermissionDetailFragment.java

private void displayApps(LinearLayout permListView, String permName) {
    List<PackageInfo> packs = mPm.getInstalledPackages(0);
    SortedMap<String, MyPermissionInfo> usedPerms = new TreeMap<String, MyPermissionInfo>();
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);/*from www  .j  a  va 2s . c  o m*/
        PackageInfo pkgInfo;
        try {
            pkgInfo = mPm.getPackageInfo(p.packageName, PackageManager.GET_PERMISSIONS);
        } catch (NameNotFoundException e) {
            Log.w(TAG, "Couldn't retrieve permissions for package:" + p.packageName);
            continue;
        }
        // Extract all user permissions
        Set<MyPermissionInfo> permSet = new HashSet<MyPermissionInfo>();
        if ((pkgInfo.applicationInfo != null) && (pkgInfo.applicationInfo.uid != -1)) {
            if (localLOGV)
                Log.w(TAG, "getAllUsedPermissions package:" + p.packageName);
            getAllUsedPermissions(pkgInfo.applicationInfo.uid, permSet);
        }
        for (MyPermissionInfo tmpInfo : permSet) {
            if (localLOGV)
                Log.i(TAG, "tmpInfo package:" + p.packageName + ", tmpInfo.name=" + tmpInfo.name);
            if (tmpInfo.name.equalsIgnoreCase(permName)) {
                if (localLOGV)
                    Log.w(TAG, "Adding package:" + p.packageName);
                tmpInfo.packageName = p.packageName;
                usedPerms.put(tmpInfo.packageName, tmpInfo);
            }
        }
    }
    mUsedPerms = usedPerms;
    permListView.removeAllViews();
    /* add the All Entry                        */
    int spacing = (int) (8 * mContext.getResources().getDisplayMetrics().density);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    int j = 0;
    for (Map.Entry<String, MyPermissionInfo> entry : usedPerms.entrySet()) {
        MyPermissionInfo tmpPerm = entry.getValue();
        if (tmpPerm.packageName.compareToIgnoreCase("com.thinkthinkdo.pff_appsettings") != 0) {
            if (localLOGV)
                Log.w(TAG, "usedPacks containd package:" + tmpPerm.packageName);
            View view = getAppItemView(mContext, mInflater, tmpPerm);
            lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (j == 0) {
                lp.topMargin = spacing;
            }
            if (permListView.getChildCount() == 0) {
                lp.topMargin *= 2;
            }
            permListView.addView(view, lp);
            j++;
        }
    }
}

From source file:org.apache.accumulo.tserver.TabletServer.java

static Value checkTabletMetadata(KeyExtent extent, TServerInstance instance,
        SortedMap<Key, Value> tabletsKeyValues, Text metadataEntry) throws AccumuloException {

    TServerInstance future = null;/* w ww . j  a v  a 2 s .com*/
    Value prevEndRow = null;
    Value dir = null;
    Value time = null;
    for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
        Key key = entry.getKey();
        if (!metadataEntry.equals(key.getRow())) {
            log.info("Unexpected row in tablet metadata " + metadataEntry + " " + key.getRow());
            return null;
        }
        Text cf = key.getColumnFamily();
        if (cf.equals(TabletsSection.FutureLocationColumnFamily.NAME)) {
            if (future != null) {
                throw new AccumuloException("Tablet has multiple future locations " + extent);
            }
            future = new TServerInstance(entry.getValue(), key.getColumnQualifier());
        } else if (cf.equals(TabletsSection.CurrentLocationColumnFamily.NAME)) {
            log.info("Tablet seems to be already assigned to "
                    + new TServerInstance(entry.getValue(), key.getColumnQualifier()));
            return null;
        } else if (TabletsSection.TabletColumnFamily.PREV_ROW_COLUMN.hasColumns(key)) {
            prevEndRow = entry.getValue();
        } else if (TabletsSection.ServerColumnFamily.DIRECTORY_COLUMN.hasColumns(key)) {
            dir = entry.getValue();
        } else if (TabletsSection.ServerColumnFamily.TIME_COLUMN.hasColumns(key)) {
            time = entry.getValue();
        }
    }

    if (prevEndRow == null) {
        throw new AccumuloException("Metadata entry does not have prev row (" + metadataEntry + ")");
    } else {
        KeyExtent ke2 = new KeyExtent(metadataEntry, prevEndRow);
        if (!extent.equals(ke2)) {
            log.info("Tablet prev end row mismatch " + extent + " " + ke2.getPrevEndRow());
            return null;
        }
    }

    if (dir == null) {
        throw new AccumuloException("Metadata entry does not have directory (" + metadataEntry + ")");
    }

    if (time == null && !extent.equals(RootTable.OLD_EXTENT)) {
        throw new AccumuloException("Metadata entry does not have time (" + metadataEntry + ")");
    }

    if (future == null) {
        log.info("The master has not assigned " + extent + " to " + instance);
        return null;
    }

    if (!instance.equals(future)) {
        log.info("Table " + extent + " has been assigned to " + future + " which is not " + instance);
        return null;
    }

    return dir;
}

From source file:com.konakart.actions.gateways.CyberSourceSAResponseAction.java

public String execute() {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();

    String transactionId = null;/*from w  w w . j a v a  2 s .  c om*/
    String merchantReference = null;
    String reasonCode = null;
    String authAmount = null;
    String decision = null;
    String referenceNumber = null;
    String customerEmail = null;
    String signature = null;
    String orderAmount = null;
    String authCode = null;
    String responseEnvironment = null;
    String authResponse = null;
    String message = null;
    String signedFields = null;

    KKAppEng kkAppEng = null;

    if (log.isDebugEnabled()) {
        log.debug(CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE + " Response Action");
    }

    SortedMap<String, String> responseHash = new TreeMap<String, String>();
    int maxParamNameSize = 0;

    try {
        // Process the parameters sent in the callback
        StringBuffer sb = new StringBuffer();
        if (request != null) {
            Enumeration<String> en = request.getParameterNames();
            while (en.hasMoreElements()) {
                String paramName = en.nextElement();
                String paramValue = request.getParameter(paramName);

                if (sb.length() > 0) {
                    sb.append("\n");
                }
                sb.append(paramName);
                sb.append(" = ");
                sb.append(paramValue);

                // Capture important variables so that we can determine whether the transaction
                // was successful
                if (paramName != null) {
                    responseHash.put(paramName, paramValue);

                    if (paramName.equalsIgnoreCase("kkret")) {
                        if (paramValue.equalsIgnoreCase("true")) {
                            return "Approved";
                        }
                        return "CheckoutError";
                    }

                    if (paramName.length() > maxParamNameSize) {
                        maxParamNameSize = paramName.length();
                    }
                }
            }
        }

        // Dump the Hashtable

        if (log.isDebugEnabled()) {
            String str = "Response fields:";

            for (Map.Entry<String, String> entry : responseHash.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();

                str += "\n    " + Utils.padRight(key, maxParamNameSize) + " = " + value;
            }

            log.debug(str);
        }

        // Pick out values of interest

        decision = responseHash.get("decision");
        transactionId = responseHash.get("transaction_id");
        merchantReference = responseHash.get("req_merchant_defined_data1");
        responseEnvironment = responseHash.get("req_merchant_defined_data2");
        authCode = responseHash.get("auth_code");
        reasonCode = responseHash.get("reason_code");
        authAmount = responseHash.get("auth_amount");
        authResponse = responseHash.get("auth_response");
        referenceNumber = responseHash.get("req_reference_number");
        orderAmount = responseHash.get("req_amount");
        signature = responseHash.get("signature");
        message = responseHash.get("message");
        signedFields = responseHash.get("signed_field_names");

        String fullGatewayResponse = sb.toString();

        if (log.isDebugEnabled()) {
            //log.debug(CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE + " Raw Response data:\n"
            //        + fullGatewayResponse);
            log.debug("\n    decision                      = " + decision
                    + "\n    message                       = " + message
                    + "\n    transactionId                 = " + transactionId
                    + "\n    merchantReference             = " + merchantReference
                    + "\n    authCode                      = " + authCode
                    + "\n    reasonCode                    = " + reasonCode
                    + "\n    authAmount                    = " + authAmount
                    + "\n    authResponse                  = " + authResponse
                    + "\n    orderAmount                   = " + orderAmount
                    + "\n    referenceNumber               = " + referenceNumber
                    + "\n    signature                     = " + signature
                    + "\n    responseEnvironment           = " + responseEnvironment);
        }

        // Pick out the values from the merchantReference
        // Split the merchantReference into orderId, orderNumber and store information
        if (merchantReference == null) {
            throw new Exception("A merchant reference wasn't received from CyberSource");
        }
        StringTokenizer st = new StringTokenizer(merchantReference, "~");
        String orderIdStr = null;
        int orderId = -1;
        String orderNumberStr = null;
        String storeId = null;
        int engineMode = -1;
        boolean customersShared = false;
        boolean productsShared = false;
        boolean categoriesShared = false;
        String countryCode = null;

        if (st.hasMoreTokens()) {
            orderIdStr = st.nextToken();
            orderId = Integer.parseInt(orderIdStr);
        }
        if (st.hasMoreTokens()) {
            orderNumberStr = st.nextToken();
        }
        if (st.hasMoreTokens()) {
            storeId = st.nextToken();
        }
        if (st.hasMoreTokens()) {
            engineMode = Integer.parseInt(st.nextToken());
        }
        if (st.hasMoreTokens()) {
            customersShared = Boolean.getBoolean(st.nextToken());
        }
        if (st.hasMoreTokens()) {
            productsShared = Boolean.getBoolean(st.nextToken());
        }
        if (st.hasMoreTokens()) {
            categoriesShared = Boolean.getBoolean(st.nextToken());
        }
        if (st.hasMoreTokens()) {
            countryCode = st.nextToken();
        }

        if (log.isDebugEnabled()) {
            log.debug("Derived from merchantReference:         \n" + "    OrderId                       = "
                    + orderId + "\n" + "    OrderNumber                   = " + orderNumberStr + "\n"
                    + "    StoreId                       = " + storeId + "\n"
                    + "    EngineMode                    = " + engineMode + "\n"
                    + "    CustomersShared               = " + customersShared + "\n"
                    + "    ProductsShared                = " + productsShared + "\n"
                    + "    CategoriesShared              = " + categoriesShared + "\n"
                    + "    CountryCode                   = " + countryCode);
        }

        // Get an instance of the KonaKart engine
        // kkAppEng = this.getKKAppEng(request); // v3.2 code
        kkAppEng = this.getKKAppEng(request, response); // v4.1 code

        int custId = this.loggedIn(request, response, kkAppEng, "Checkout", false, null);

        // Check to see whether the user is logged in
        if (custId < 0) {
            if (log.isInfoEnabled()) {
                log.info("Customer is not logged in");
            }
            return KKLOGIN;
        }

        // If we didn't receive a decision, we log a warning and return
        if (decision == null) {
            String msg = "No decision returned for the " + CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE + " module";
            saveIPNrecord(kkAppEng, orderId, CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE, fullGatewayResponse,
                    decision, transactionId, RET1_DESC + msg, RET1);
            throw new Exception(msg);
        }

        String sharedSecret = kkAppEng.getCustomConfig(orderIdStr + "-CUSTOM1", true);

        boolean validateSignature = false;

        if (sharedSecret == null) {
            if (log.isWarnEnabled()) {
                log.warn("CyberSource shared secret is null");
            }
            validateSignature = false;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Shared Secret found on session for order " + orderIdStr);
            }
            // Check the authenticity of the message by checking the signature

            String data = null;
            String dataStr = "Data to Sign:";

            for (String field : signedFields.split(",")) {
                if (data == null) {
                    data = field + "=" + responseHash.get(field);
                } else {
                    data += "," + field + "=" + responseHash.get(field);
                }
                dataStr += "\n    " + Utils.padRight(field, maxParamNameSize) + " = " + responseHash.get(field);
            }

            if (log.isDebugEnabled()) {
                log.debug("Validate Signed Fields : " + data);
                log.debug(dataStr);
            }

            validateSignature = CyberSourceSAHMACTools.verifyBase64EncodedSignature(sharedSecret, signature,
                    data);
        }

        if (log.isDebugEnabled()) {
            log.debug("Signature Validation Result: " + validateSignature);
        }

        // If the signature on the amount doesn't validate we log a warning and return
        if (!validateSignature) {
            String msg = "Signature Validation Failed for the " + CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE
                    + " module - orderId " + orderId;
            saveIPNrecord(kkAppEng, orderId, CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE, fullGatewayResponse,
                    decision, transactionId, RET1_DESC + msg, RET1);
            throw new Exception(msg);
        }

        // Validate we are in the correct environment
        boolean validateEnvironment = true;
        String sessionEnvironment = kkAppEng.getCustomConfig(orderIdStr + "-CUSTOM2", true);
        if (sessionEnvironment == null) {
            if (log.isWarnEnabled()) {
                log.warn("CyberSource Operating Environment is null on the session");
            }
            validateEnvironment = false;
        } else if (illegalEnvironmentValue(sessionEnvironment)) {
            if (log.isWarnEnabled()) {
                log.warn("CyberSource Operating Environment on the session is illegal");
            }
            validateEnvironment = false;
        } else if (illegalEnvironmentValue(responseEnvironment)) {
            if (log.isWarnEnabled()) {
                log.warn("CyberSource Operating Environment in the session is illegal");
            }
            validateEnvironment = false;
        } else if (!sessionEnvironment.equals(responseEnvironment)) {
            if (log.isWarnEnabled()) {
                log.warn("CyberSource Operating Environment in the session (" + sessionEnvironment
                        + ") does not match that in the response (" + responseEnvironment + ")");
            }
            validateEnvironment = false;
        }

        // If the signature on the amount doesn't validate we log a warning and return
        if (!validateEnvironment) {
            String msg = "Environment Validation Failed for the " + CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE
                    + " module - orderId " + orderId;
            saveIPNrecord(kkAppEng, orderId, CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE, fullGatewayResponse,
                    decision, transactionId, RET1_DESC + msg, RET1);
            throw new Exception(msg);
        }

        // See if we need to send an email, by looking at the configuration
        String sendEmailsConfig = kkAppEng.getConfig(ConfigConstants.SEND_EMAILS);
        boolean sendEmail = false;
        if (sendEmailsConfig != null && sendEmailsConfig.equalsIgnoreCase("true")) {
            sendEmail = true;
        }

        // If we didn't receive an ACCEPT decision, we let the user Try Again

        OrderUpdateIf updateOrder = new OrderUpdate();
        updateOrder.setUpdatedById(kkAppEng.getActiveCustId());

        if (!decision.equals("ACCEPT")) {
            if (log.isDebugEnabled()) {
                log.debug("Payment Not Approved for orderId: " + orderId + " for customer: " + customerEmail
                        + " reason: " + getReasonDescription(reasonCode));
            }
            saveIPNrecord(kkAppEng, orderId, CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE, fullGatewayResponse,
                    decision, transactionId, RET2_DESC + decision + " : " + getReasonDescription(reasonCode),
                    RET2);

            String comment = ORDER_HISTORY_COMMENT_KO + decision;
            kkAppEng.getEng().updateOrder(kkAppEng.getSessionId(), orderId,
                    com.konakart.bl.OrderMgr.PAYMENT_DECLINED_STATUS, sendEmail, comment, updateOrder);
            if (sendEmail) {
                sendOrderConfirmationMail(kkAppEng, orderId, /* success */false);
            }

            String msg = kkAppEng.getMsg("checkout.cc.gateway.error", new String[] { comment });
            addActionError(msg);

            /*
             * Add a parameter that is sent back to this action when trying to break out of the
             * frame
             */
            if (request != null) {
                StringBuffer retUrl = request.getRequestURL();
                retUrl.append("?kkret=false");
                this.url = retUrl.toString();
                return "redirect";
            }
            throw new KKException("The HttpServletRequest is null");
        }

        // If successful, we forward to "Approved"

        if (log.isDebugEnabled()) {
            log.debug("Payment Approved for orderId " + orderId + " for customer " + customerEmail);
        }
        saveIPNrecord(kkAppEng, orderId, CyberSourceSA.CYBERSOURCESA_GATEWAY_CODE, fullGatewayResponse,
                decision, transactionId, RET0_DESC, RET0);

        String comment = ORDER_HISTORY_COMMENT_OK + transactionId;
        kkAppEng.getEng().updateOrder(kkAppEng.getSessionId(), orderId,
                com.konakart.bl.OrderMgr.PAYMENT_RECEIVED_STATUS, sendEmail, comment, updateOrder);

        // If the order payment was approved we update the inventory
        kkAppEng.getEng().updateInventory(kkAppEng.getSessionId(), orderId);

        if (sendEmail) {
            sendOrderConfirmationMail(kkAppEng, orderId, /* success */true);
        }

        // If we received no exceptions, delete the basket
        kkAppEng.getBasketMgr().emptyBasket();

        /*
         * Add a parameter that is sent back to this action when trying to break out of the
         * frame
         */
        if (request != null) {
            StringBuffer retUrl = request.getRequestURL();
            retUrl.append("?kkret=true");
            this.url = retUrl.toString();
            return "redirect";
        }
        throw new KKException("The HttpServletRequest is null");

    } catch (Exception e) {
        e.printStackTrace();
        return super.handleException(request, e);
    }
}

From source file:org.apache.hadoop.hbase.master.RegionManager.java

/**
 * Applies actions specific to a column family within a region.
 */// w  ww  . j ava2  s  .  c o  m
private void applyCFActions(final HServerInfo serverInfo, final ArrayList<HMsg> returnMsgs,
        final SortedMap<byte[], SortedMap<byte[], Pair<HRegionInfo, HServerAddress>>> map,
        final HMsg.Type msg) {
    HServerAddress addr = serverInfo.getServerAddress();
    synchronized (map) {
        Iterator<SortedMap<byte[], Pair<HRegionInfo, HServerAddress>>> it1 = map.values().iterator();
        while (it1.hasNext()) {
            SortedMap<byte[], Pair<HRegionInfo, HServerAddress>> cfMap = it1.next();
            Iterator<Map.Entry<byte[], Pair<HRegionInfo, HServerAddress>>> it2 = cfMap.entrySet().iterator();
            while (it2.hasNext()) {
                Map.Entry mapPairs = it2.next();
                Pair<HRegionInfo, HServerAddress> pair = (Pair<HRegionInfo, HServerAddress>) mapPairs
                        .getValue();
                if (addr.equals(pair.getSecond())) {
                    byte[] columnFamily = (byte[]) mapPairs.getKey();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Sending " + msg + " " + pair.getFirst() + " to " + addr
                                + " for column family : " + new String(columnFamily));
                    }
                    returnMsgs.add(new HMsg(msg, pair.getFirst(), columnFamily));
                    it2.remove();
                }
            }
            if (cfMap.isEmpty()) {
                // If entire map is empty, remove it from the parent map.
                it1.remove();
            }
        }
    }
}

From source file:org.torproject.ernie.web.ExoneraTorServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    /* Start writing response. */
    PrintWriter out = response.getWriter();
    writeHeader(out);//from   www. j  a  va2s . com

    /* Look up first and last consensus in the database. */
    long firstValidAfter = -1L, lastValidAfter = -1L;
    try {
        Connection conn = this.ds.getConnection();
        Statement statement = conn.createStatement();
        String query = "SELECT MIN(validafter) AS first, " + "MAX(validafter) AS last FROM consensus";
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            firstValidAfter = rs.getTimestamp(1).getTime();
            lastValidAfter = rs.getTimestamp(2).getTime();
        }
        rs.close();
        statement.close();
        conn.close();
    } catch (SQLException e) {
        /* Looks like we don't have any consensuses. */
    }
    if (firstValidAfter < 0L || lastValidAfter < 0L) {
        out.println("<p><font color=\"red\"><b>Warning: </b></font>This "
                + "server doesn't have any relay lists available. If this " + "problem persists, please "
                + "<a href=\"mailto:tor-assistants@freehaven.net\">let us " + "know</a>!</p>\n");
        writeFooter(out);
        return;
    }

    out.println("<a name=\"relay\"></a><h3>Was there a Tor relay running " + "on this IP address?</h3>");

    /* Parse IP parameter. */
    Pattern ipAddressPattern = Pattern
            .compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
                    + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    String ipParameter = request.getParameter("ip");
    String relayIP = "", ipWarning = "";
    if (ipParameter != null && ipParameter.length() > 0) {
        Matcher ipParameterMatcher = ipAddressPattern.matcher(ipParameter);
        if (ipParameterMatcher.matches()) {
            String[] ipParts = ipParameter.split("\\.");
            relayIP = Integer.parseInt(ipParts[0]) + "." + Integer.parseInt(ipParts[1]) + "."
                    + Integer.parseInt(ipParts[2]) + "." + Integer.parseInt(ipParts[3]);
        } else {
            ipWarning = "\""
                    + (ipParameter.length() > 20 ? ipParameter.substring(0, 20) + "[...]" : ipParameter)
                    + "\" is not a valid IP address.";
        }
    }

    /* Parse timestamp parameter. */
    String timestampParameter = request.getParameter("timestamp");
    long timestamp = 0L;
    String timestampStr = "", timestampWarning = "";
    SimpleDateFormat shortDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    shortDateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    if (timestampParameter != null && timestampParameter.length() > 0) {
        try {
            timestamp = shortDateTimeFormat.parse(timestampParameter).getTime();
            timestampStr = shortDateTimeFormat.format(timestamp);
            if (timestamp < firstValidAfter || timestamp > lastValidAfter) {
                timestampWarning = "Please pick a value between \""
                        + shortDateTimeFormat.format(firstValidAfter) + "\" and \""
                        + shortDateTimeFormat.format(lastValidAfter) + "\".";
            }
        } catch (ParseException e) {
            /* We have no way to handle this exception, other than leaving
               timestampStr at "". */
            timestampWarning = "\""
                    + (timestampParameter.length() > 20 ? timestampParameter.substring(0, 20) + "[...]"
                            : timestampParameter)
                    + "\" is not a valid timestamp.";
        }
    }

    /* If either IP address or timestamp is provided, the other one must
     * be provided, too. */
    if (relayIP.length() < 1 && timestampStr.length() > 0 && ipWarning.length() < 1) {
        ipWarning = "Please provide an IP address.";
    }
    if (relayIP.length() > 0 && timestampStr.length() < 1 && timestampWarning.length() < 1) {
        timestampWarning = "Please provide a timestamp.";
    }

    /* Parse target IP parameter. */
    String targetIP = "", targetPort = "", target = "";
    String[] targetIPParts = null;
    String targetAddrParameter = request.getParameter("targetaddr");
    String targetAddrWarning = "";
    if (targetAddrParameter != null && targetAddrParameter.length() > 0) {
        Matcher targetAddrParameterMatcher = ipAddressPattern.matcher(targetAddrParameter);
        if (targetAddrParameterMatcher.matches()) {
            String[] targetAddrParts = targetAddrParameter.split("\\.");
            targetIP = Integer.parseInt(targetAddrParts[0]) + "." + Integer.parseInt(targetAddrParts[1]) + "."
                    + Integer.parseInt(targetAddrParts[2]) + "." + Integer.parseInt(targetAddrParts[3]);
            target = targetIP;
            targetIPParts = targetIP.split("\\.");
        } else {
            targetAddrWarning = "\""
                    + (targetAddrParameter.length() > 20 ? timestampParameter.substring(0, 20) + "[...]"
                            : timestampParameter)
                    + "\" is not a valid IP address.";
        }
    }

    /* Parse target port parameter. */
    String targetPortParameter = request.getParameter("targetport");
    String targetPortWarning = "";
    if (targetPortParameter != null && targetPortParameter.length() > 0) {
        Pattern targetPortPattern = Pattern.compile("\\d+");
        if (targetPortParameter.length() < 5 && targetPortPattern.matcher(targetPortParameter).matches()
                && !targetPortParameter.equals("0") && Integer.parseInt(targetPortParameter) < 65536) {
            targetPort = targetPortParameter;
            if (target != null) {
                target += ":" + targetPort;
            } else {
                target = targetPort;
            }
        } else {
            targetPortWarning = "\""
                    + (targetPortParameter.length() > 8 ? targetPortParameter.substring(0, 8) + "[...]"
                            : targetPortParameter)
                    + "\" is not a valid TCP port.";
        }
    }

    /* If target port is provided, a target address must be provided,
     * too. */
    if (targetPort.length() > 0 && targetIP.length() < 1 && targetAddrWarning.length() < 1) {
        targetAddrWarning = "Please provide an IP address.";
    }

    /* Write form with IP address and timestamp. */
    out.println("        <form action=\"exonerator.html#relay\">\n"
            + "          <input type=\"hidden\" name=\"targetaddr\" "
            + (targetIP.length() > 0 ? " value=\"" + targetIP + "\"" : "") + ">\n"
            + "          <input type=\"hidden\" name=\"targetPort\""
            + (targetPort.length() > 0 ? " value=\"" + targetPort + "\"" : "") + ">\n" + "          <table>\n"
            + "            <tr>\n" + "              <td align=\"right\">IP address in question:" + "</td>\n"
            + "              <td><input type=\"text\" name=\"ip\""
            + (relayIP.length() > 0 ? " value=\"" + relayIP + "\"" : "") + ">"
            + (ipWarning.length() > 0 ? "<br><font color=\"red\">" + ipWarning + "</font>" : "") + "</td>\n"
            + "              <td><i>(Ex.: 1.2.3.4)</i></td>\n" + "            </tr>\n" + "            <tr>\n"
            + "              <td align=\"right\">Timestamp, in UTC:</td>\n"
            + "              <td><input type=\"text\" name=\"timestamp\""
            + (timestampStr.length() > 0 ? " value=\"" + timestampStr + "\"" : "") + ">"
            + (timestampWarning.length() > 0 ? "<br><font color=\"red\">" + timestampWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 2010-01-01 12:00)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td></td>\n" + "              <td>\n"
            + "                <input type=\"submit\">\n" + "                <input type=\"reset\">\n"
            + "              </td>\n" + "              <td></td>\n" + "            </tr>\n"
            + "          </table>\n" + "        </form>\n");

    if (relayIP.length() < 1 || timestampStr.length() < 1) {
        writeFooter(out);
        return;
    }

    /* Look up relevant consensuses. */
    long timestampTooOld = timestamp - 15L * 60L * 60L * 1000L;
    long timestampFrom = timestamp - 3L * 60L * 60L * 1000L;
    long timestampTooNew = timestamp + 12L * 60L * 60L * 1000L;
    out.printf(
            "<p>Looking up IP address %s in the relay lists published " + "between %s and %s. "
                    + "Clients could have used any of these relay lists to "
                    + "select relays for their paths and build circuits using them. "
                    + "You may follow the links to relay lists and relay descriptors "
                    + "to grep for the lines printed below and confirm that results " + "are correct.<br>",
            relayIP, shortDateTimeFormat.format(timestampFrom), timestampStr);
    SimpleDateFormat validAfterTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    validAfterTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String fromValidAfter = validAfterTimeFormat.format(timestampTooOld);
    String toValidAfter = validAfterTimeFormat.format(timestampTooNew);
    SortedMap<Long, String> tooOldConsensuses = new TreeMap<Long, String>();
    SortedMap<Long, String> relevantConsensuses = new TreeMap<Long, String>();
    SortedMap<Long, String> tooNewConsensuses = new TreeMap<Long, String>();
    try {
        Connection conn = this.ds.getConnection();
        Statement statement = conn.createStatement();
        String query = "SELECT validafter, rawdesc FROM consensus " + "WHERE validafter >= '" + fromValidAfter
                + "' AND validafter <= '" + toValidAfter + "'";
        ResultSet rs = statement.executeQuery(query);
        while (rs.next()) {
            long consensusTime = rs.getTimestamp(1).getTime();
            String rawConsensusString = new String(rs.getBytes(2), "US-ASCII");
            if (consensusTime < timestampFrom) {
                tooOldConsensuses.put(consensusTime, rawConsensusString);
            } else if (consensusTime > timestamp) {
                tooNewConsensuses.put(consensusTime, rawConsensusString);
            } else {
                relevantConsensuses.put(consensusTime, rawConsensusString);
            }
        }
        rs.close();
        statement.close();
        conn.close();
    } catch (SQLException e) {
        /* Looks like we don't have any consensuses in the requested
           interval. */
    }
    SortedMap<Long, String> allConsensuses = new TreeMap<Long, String>();
    allConsensuses.putAll(tooOldConsensuses);
    allConsensuses.putAll(relevantConsensuses);
    allConsensuses.putAll(tooNewConsensuses);
    if (allConsensuses.isEmpty()) {
        out.println("        <p>No relay lists found!</p>\n" + "        <p>Result is INDECISIVE!</p>\n"
                + "        <p>We cannot make any statement whether there was "
                + "a Tor relay running on IP address " + relayIP + " at " + timestampStr + "! We "
                + "did not find any relevant relay lists preceding the given "
                + "time. If you think this is an error on our side, please "
                + "<a href=\"mailto:tor-assistants@freehaven.net\">contact " + "us</a>!</p>\n");
        writeFooter(out);
        return;
    }

    /* Parse consensuses to find descriptors belonging to the IP
       address. */
    SortedSet<Long> positiveConsensusesNoTarget = new TreeSet<Long>();
    Set<String> addressesInSameNetwork = new HashSet<String>();
    SortedMap<String, Set<Long>> relevantDescriptors = new TreeMap<String, Set<Long>>();
    SimpleDateFormat validAfterUrlFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    validAfterUrlFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    for (Map.Entry<Long, String> e : allConsensuses.entrySet()) {
        long consensus = e.getKey();
        if (relevantConsensuses.containsKey(consensus)) {
            long validAfterTime = -1L;
            String validAfterDatetime = validAfterTimeFormat.format(consensus);
            String validAfterString = validAfterUrlFormat.format(consensus);
            out.println("        <br><tt>valid-after <b>" + "<a href=\"consensus?valid-after="
                    + validAfterString + "\" target=\"_blank\">" + validAfterDatetime + "</b></a></tt><br>");
        }
        String rawConsensusString = e.getValue();
        BufferedReader br = new BufferedReader(new StringReader(rawConsensusString));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (!line.startsWith("r ")) {
                continue;
            }
            String[] parts = line.split(" ");
            String address = parts[6];
            if (address.equals(relayIP)) {
                String hex = String.format("%040x", new BigInteger(1, Base64.decodeBase64(parts[3] + "==")));
                if (!relevantDescriptors.containsKey(hex)) {
                    relevantDescriptors.put(hex, new HashSet<Long>());
                }
                relevantDescriptors.get(hex).add(consensus);
                positiveConsensusesNoTarget.add(consensus);
                if (relevantConsensuses.containsKey(consensus)) {
                    out.println("    <tt>r " + parts[1] + " " + parts[2] + " " + "<a href=\"serverdesc?desc-id="
                            + hex + "\" " + "target=\"_blank\">" + parts[3] + "</a> " + parts[4] + " "
                            + parts[5] + " <b>" + parts[6] + "</b> " + parts[7] + " " + parts[8] + "</tt><br>");
                }
            } else {
                if (relayIP.startsWith(address.substring(0, address.lastIndexOf(".")))) {
                    addressesInSameNetwork.add(address);
                }
            }
        }
        br.close();
    }
    if (relevantDescriptors.isEmpty()) {
        out.printf(
                "        <p>None found!</p>\n" + "        <p>Result is NEGATIVE with moderate certainty!</p>\n"
                        + "        <p>We did not find IP " + "address " + relayIP
                        + " in any of the relay lists that were "
                        + "published between %s and %s.\n\nA possible "
                        + "reason for false negatives is that the relay is using a "
                        + "different IP address when generating a descriptor than for "
                        + "exiting to the Internet. We hope to provide better checks "
                        + "for this case in the future.</p>\n",
                shortDateTimeFormat.format(timestampTooOld), shortDateTimeFormat.format(timestampTooNew));
        if (!addressesInSameNetwork.isEmpty()) {
            out.println("        <p>The following other IP addresses of Tor "
                    + "relays were found in the mentioned relay lists that "
                    + "are in the same /24 network and that could be related to " + "IP address " + relayIP
                    + ":</p>\n");
            for (String s : addressesInSameNetwork) {
                out.println("        <p>" + s + "</p>\n");
            }
        }
        writeFooter(out);
        return;
    }

    /* Print out result. */
    Set<Long> matches = positiveConsensusesNoTarget;
    if (matches.contains(relevantConsensuses.lastKey())) {
        out.println("        <p>Result is POSITIVE with high certainty!" + "</p>\n"
                + "        <p>We found one or more relays on IP address " + relayIP
                + " in the most recent relay list preceding " + timestampStr
                + " that clients were likely to know.</p>\n");
    } else {
        boolean inOtherRelevantConsensus = false, inTooOldConsensuses = false, inTooNewConsensuses = false;
        for (long match : matches) {
            if (relevantConsensuses.containsKey(match)) {
                inOtherRelevantConsensus = true;
            } else if (tooOldConsensuses.containsKey(match)) {
                inTooOldConsensuses = true;
            } else if (tooNewConsensuses.containsKey(match)) {
                inTooNewConsensuses = true;
            }
        }
        if (inOtherRelevantConsensus) {
            out.println("        <p>Result is POSITIVE " + "with moderate certainty!</p>\n");
            out.println("<p>We found one or more relays on IP address " + relayIP
                    + ", but not in the relay list immediately " + "preceding " + timestampStr
                    + ". A possible reason for the "
                    + "relay being missing in the last relay list preceding the "
                    + "given time might be that some of the directory "
                    + "authorities had difficulties connecting to the relay. "
                    + "However, clients might still have used the relay.</p>\n");
        } else {
            out.println("        <p>Result is NEGATIVE " + "with high certainty!</p>\n");
            out.println("        <p>We did not find any relay on IP address " + relayIP
                    + " in the relay lists 3 hours preceding " + timestampStr + ".</p>\n");
            if (inTooOldConsensuses || inTooNewConsensuses) {
                if (inTooOldConsensuses && !inTooNewConsensuses) {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published between 5 and 3 " + "hours before "
                            + timestampStr + ".</p>\n");
                } else if (!inTooOldConsensuses && inTooNewConsensuses) {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published up to 2 hours " + "after " + timestampStr
                            + ".</p>\n");
                } else {
                    out.println("        <p>Note that we found a matching relay "
                            + "in relay lists that were published between 5 and 3 "
                            + "hours before and in relay lists that were published " + "up to 2 hours after "
                            + timestampStr + ".</p>\n");
                }
                out.println("<p>Make sure that the timestamp you provided is "
                        + "in the correct timezone: UTC (or GMT).</p>");
            }
            writeFooter(out);
            return;
        }
    }

    /* Second part: target */
    out.println("<br><a name=\"exit\"></a><h3>Was this relay configured "
            + "to permit exiting to a given target?</h3>");

    out.println("        <form action=\"exonerator.html#exit\">\n"
            + "              <input type=\"hidden\" name=\"timestamp\"\n" + "                         value=\""
            + timestampStr + "\">\n" + "              <input type=\"hidden\" name=\"ip\" " + "value=\""
            + relayIP + "\">\n" + "          <table>\n" + "            <tr>\n"
            + "              <td align=\"right\">Target address:</td>\n"
            + "              <td><input type=\"text\" name=\"targetaddr\""
            + (targetIP.length() > 0 ? " value=\"" + targetIP + "\"" : "") + "\">"
            + (targetAddrWarning.length() > 0 ? "<br><font color=\"red\">" + targetAddrWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 4.3.2.1)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td align=\"right\">Target port:</td>\n"
            + "              <td><input type=\"text\" name=\"targetport\""
            + (targetPort.length() > 0 ? " value=\"" + targetPort + "\"" : "") + ">"
            + (targetPortWarning.length() > 0 ? "<br><font color=\"red\">" + targetPortWarning + "</font>" : "")
            + "</td>\n" + "              <td><i>(Ex.: 80)</i></td>\n" + "            </tr>\n"
            + "            <tr>\n" + "              <td></td>\n" + "              <td>\n"
            + "                <input type=\"submit\">\n" + "                <input type=\"reset\">\n"
            + "              </td>\n" + "              <td></td>\n" + "            </tr>\n"
            + "          </table>\n" + "        </form>\n");

    if (targetIP.length() < 1) {
        writeFooter(out);
        return;
    }

    /* Parse router descriptors to check exit policies. */
    out.println("<p>Searching the relay descriptors published by the " + "relay on IP address " + relayIP
            + " to find out whether this " + "relay permitted exiting to " + target + ". You may follow the "
            + "links above to the relay descriptors and grep them for the "
            + "lines printed below to confirm that results are correct.</p>");
    SortedSet<Long> positiveConsensuses = new TreeSet<Long>();
    Set<String> missingDescriptors = new HashSet<String>();
    Set<String> descriptors = relevantDescriptors.keySet();
    for (String descriptor : descriptors) {
        byte[] rawDescriptor = null;
        try {
            Connection conn = this.ds.getConnection();
            Statement statement = conn.createStatement();
            String query = "SELECT rawdesc FROM descriptor " + "WHERE descriptor = '" + descriptor + "'";
            ResultSet rs = statement.executeQuery(query);
            if (rs.next()) {
                rawDescriptor = rs.getBytes(1);
            }
            rs.close();
            statement.close();
            conn.close();
        } catch (SQLException e) {
            /* Consider this descriptors as 'missing'. */
            continue;
        }
        if (rawDescriptor != null && rawDescriptor.length > 0) {
            missingDescriptors.remove(descriptor);
            String rawDescriptorString = new String(rawDescriptor, "US-ASCII");
            try {
                BufferedReader br = new BufferedReader(new StringReader(rawDescriptorString));
                String line = null, routerLine = null, publishedLine = null;
                StringBuilder acceptRejectLines = new StringBuilder();
                boolean foundMatch = false;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith("router ")) {
                        routerLine = line;
                    } else if (line.startsWith("published ")) {
                        publishedLine = line;
                    } else if (line.startsWith("reject ") || line.startsWith("accept ")) {
                        if (foundMatch) {
                            out.println("<tt> " + line + "</tt><br>");
                            continue;
                        }
                        boolean ruleAccept = line.split(" ")[0].equals("accept");
                        String ruleAddress = line.split(" ")[1].split(":")[0];
                        if (!ruleAddress.equals("*")) {
                            if (!ruleAddress.contains("/") && !ruleAddress.equals(targetIP)) {
                                /* IP address does not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                            String[] ruleIPParts = ruleAddress.split("/")[0].split("\\.");
                            int ruleNetwork = ruleAddress.contains("/")
                                    ? Integer.parseInt(ruleAddress.split("/")[1])
                                    : 32;
                            for (int i = 0; i < 4; i++) {
                                if (ruleNetwork == 0) {
                                    break;
                                } else if (ruleNetwork >= 8) {
                                    if (ruleIPParts[i].equals(targetIPParts[i])) {
                                        ruleNetwork -= 8;
                                    } else {
                                        break;
                                    }
                                } else {
                                    int mask = 255 ^ 255 >>> ruleNetwork;
                                    if ((Integer.parseInt(ruleIPParts[i])
                                            & mask) == (Integer.parseInt(targetIPParts[i]) & mask)) {
                                        ruleNetwork = 0;
                                    }
                                    break;
                                }
                            }
                            if (ruleNetwork > 0) {
                                /* IP address does not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                        }
                        String rulePort = line.split(" ")[1].split(":")[1];
                        if (targetPort.length() < 1 && !ruleAccept && !rulePort.equals("*")) {
                            /* With no port given, we only consider reject :* rules as
                               matching. */
                            acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                            continue;
                        }
                        if (targetPort.length() > 0 && !rulePort.equals("*") && rulePort.contains("-")) {
                            int fromPort = Integer.parseInt(rulePort.split("-")[0]);
                            int toPort = Integer.parseInt(rulePort.split("-")[1]);
                            int targetPortInt = Integer.parseInt(targetPort);
                            if (targetPortInt < fromPort || targetPortInt > toPort) {
                                /* Port not contained in interval. */
                                continue;
                            }
                        }
                        if (targetPort.length() > 0) {
                            if (!rulePort.equals("*") && !rulePort.contains("-")
                                    && !targetPort.equals(rulePort)) {
                                /* Ports do not match. */
                                acceptRejectLines.append("<tt> " + line + "</tt><br>\n");
                                continue;
                            }
                        }
                        boolean relevantMatch = false;
                        for (long match : relevantDescriptors.get(descriptor)) {
                            if (relevantConsensuses.containsKey(match)) {
                                relevantMatch = true;
                            }
                        }
                        if (relevantMatch) {
                            String[] routerParts = routerLine.split(" ");
                            out.println("<br><tt>" + routerParts[0] + " " + routerParts[1] + " <b>"
                                    + routerParts[2] + "</b> " + routerParts[3] + " " + routerParts[4] + " "
                                    + routerParts[5] + "</tt><br>");
                            String[] publishedParts = publishedLine.split(" ");
                            out.println("<tt>" + publishedParts[0] + " <b>" + publishedParts[1] + " "
                                    + publishedParts[2] + "</b></tt><br>");
                            out.println(acceptRejectLines.toString());
                            out.println("<tt><b>" + line + "</b></tt><br>");
                            foundMatch = true;
                        }
                        if (ruleAccept) {
                            positiveConsensuses.addAll(relevantDescriptors.get(descriptor));
                        }
                    }
                }
                br.close();
            } catch (IOException e) {
                /* Could not read descriptor string. */
                continue;
            }
        }
    }

    /* Print out result. */
    matches = positiveConsensuses;
    if (matches.contains(relevantConsensuses.lastKey())) {
        out.println("        <p>Result is POSITIVE with high certainty!</p>" + "\n"
                + "        <p>We found one or more relays on IP address " + relayIP + " permitting exit to "
                + target + " in the most recent relay list preceding " + timestampStr
                + " that clients were likely to know.</p>\n");
        writeFooter(out);
        return;
    }
    boolean resultIndecisive = target.length() > 0 && !missingDescriptors.isEmpty();
    if (resultIndecisive) {
        out.println("        <p>Result is INDECISIVE!</p>\n"
                + "        <p>At least one referenced descriptor could not be "
                + "found. This is a rare case, but one that (apparently) "
                + "happens. We cannot make any good statement about exit "
                + "relays without these descriptors. The following descriptors " + "are missing:</p>");
        for (String desc : missingDescriptors)
            out.println("        <p>" + desc + "</p>\n");
    }
    boolean inOtherRelevantConsensus = false, inTooOldConsensuses = false, inTooNewConsensuses = false;
    for (long match : matches) {
        if (relevantConsensuses.containsKey(match)) {
            inOtherRelevantConsensus = true;
        } else if (tooOldConsensuses.containsKey(match)) {
            inTooOldConsensuses = true;
        } else if (tooNewConsensuses.containsKey(match)) {
            inTooNewConsensuses = true;
        }
    }
    if (inOtherRelevantConsensus) {
        if (!resultIndecisive) {
            out.println("        <p>Result is POSITIVE " + "with moderate certainty!</p>\n");
        }
        out.println("<p>We found one or more relays on IP address " + relayIP + " permitting exit to " + target
                + ", but not in " + "the relay list immediately preceding " + timestampStr
                + ". A possible reason for the relay being missing in the last "
                + "relay list preceding the given time might be that some of "
                + "the directory authorities had difficulties connecting to "
                + "the relay. However, clients might still have used the " + "relay.</p>\n");
    } else {
        if (!resultIndecisive) {
            out.println("        <p>Result is NEGATIVE " + "with high certainty!</p>\n");
        }
        out.println("        <p>We did not find any relay on IP address " + relayIP + " permitting exit to "
                + target + " in the relay list 3 hours preceding " + timestampStr + ".</p>\n");
        if (inTooOldConsensuses || inTooNewConsensuses) {
            if (inTooOldConsensuses && !inTooNewConsensuses) {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published between 5 and 3 " + "hours before " + timestampStr
                        + ".</p>\n");
            } else if (!inTooOldConsensuses && inTooNewConsensuses) {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published up to 2 hours after " + timestampStr + ".</p>\n");
            } else {
                out.println("        <p>Note that we found a matching relay in "
                        + "relay lists that were published between 5 and 3 "
                        + "hours before and in relay lists that were published up " + "to 2 hours after "
                        + timestampStr + ".</p>\n");
            }
            out.println("<p>Make sure that the timestamp you provided is "
                    + "in the correct timezone: UTC (or GMT).</p>");
        }
    }
    if (target != null) {
        if (positiveConsensuses.isEmpty() && !positiveConsensusesNoTarget.isEmpty()) {
            out.println("        <p>Note that although the found relay(s) did " + "not permit exiting to "
                    + target + ", there have been one " + "or more relays running at the given time.</p>");
        }
    }

    /* Finish writing response. */
    writeFooter(out);
}

From source file:org.apache.hadoop.hbase.master.RegionManager.java

private int computeNextHeaviestLoad(HServerLoad referenceLoad, HServerLoad heavierLoad) {

    SortedMap<HServerLoad, Collection<String>> heavyServers = master.getServerManager().getServersToLoad()
            .getHeavyServers(referenceLoad);
    int nservers = 0;
    for (Map.Entry<HServerLoad, Collection<String>> e : heavyServers.entrySet()) {
        Collection<String> servers = e.getValue();
        nservers += servers.size();/*from   ww w. j  a  va2  s.c  o  m*/
        if (e.getKey().compareTo(referenceLoad) == 0) {
            // This is the load factor of the server we are considering
            nservers -= 1;
            continue;
        }

        // If we get here, we are at the first load entry that is a
        // heavier load than the server we are considering
        heavierLoad.setNumberOfRequests(e.getKey().getNumberOfRequests());
        heavierLoad.setNumberOfRegions(e.getKey().getNumberOfRegions());
        break;
    }
    return nservers;
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Verify that two SortedMaps are deeply equivalent.
 *
 * @param expected//ww  w . j  ava 2s .c o m
 * @param actual
 */
private static <K, V> void _assertEquals(SortedMap<K, V> expected, SortedMap<K, V> actual) {
    _assertEquals((Map<K, V>) expected, (Map<K, V>) actual);

    // verify the order of the associated collections
    assertEquals(expected.keySet().toArray(), actual.keySet().toArray());
    assertEquals(expected.entrySet().toArray(), actual.entrySet().toArray());
    assertEquals(expected.values().toArray(), actual.values().toArray());
}

From source file:org.segrada.servlet.SegradaSimplePageCachingFilter.java

/**
 * calculate key for page from httpRequest
 * @param httpRequest the request/*w w w  .j ava 2  s  . co  m*/
 * @return cache key
 */
protected String calculateKey(HttpServletRequest httpRequest) {
    HttpSession session = httpRequest.getSession();

    // get language from session
    Object lObject = session.getAttribute("language");
    String language = lObject == null ? null : (String) lObject;
    if (language == null)
        language = httpRequest.getLocale().getLanguage();

    // get url, context path stripped
    String urlPart = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());

    // filter out jsessionid URL addition, just in case - should not happen, because it is not clean, but nevertheless
    urlPart = jSessionFilter.matcher(urlPart).replaceFirst(""); //TODO: might be faster to use lastIndexOf and substr to replace this instead of regex

    // query data map - later to be sorted into a key
    SortedMap<String, String> queryData = new TreeMap<>();

    // match with urls that require session key addition to cache in order to function properly?
    MatchResult matchResult = addSessionToCacheKey.matcher(urlPart).toMatchResult();
    if (((Matcher) matchResult).find()) {
        // this is the same as the session key
        String controller = matchResult.group().substring(1, 2).toUpperCase() + matchResult.group().substring(2)
                + "Service";
        // get session data
        Object controllerData = session.getAttribute(controller);

        // create sorted map
        if (controllerData != null && controllerData instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> d = (Map<String, Object>) controllerData;
            for (Map.Entry<String, Object> entry : d.entrySet()) {
                Object o = entry.getValue();
                String value;
                // concatenate string array in order to make caching work
                if (o instanceof String[])
                    value = String.join(",", (String[]) o);
                else // all other cases: convert to string
                    value = entry.getValue().toString();
                queryData.put(entry.getKey(), value);
            }
        }
    }

    // get query data and add it to list (overwrite session, if needed)
    boolean clearTags = false; // flag to check whether parameter clearTags was sent in form data
    boolean tagsSetByForm = false; // flag to check whether there has been a field tags in form data
    for (Map.Entry<String, String[]> parameter : httpRequest.getParameterMap().entrySet()) {
        String key = parameter.getKey();
        if (key.equals("clearTags")) {
            clearTags = true;
            continue; // do not add to parameters
        }
        if (key.equals("tags"))
            tagsSetByForm = true; // tags were in form data
        queryData.put(key, String.join(",", parameter.getValue()));
    }
    // did we have field clearTags in form, but no tags were set? => delete tags saved in session, if needed
    if (clearTags && !tagsSetByForm)
        queryData.remove("tags"); // will be removed from session via controller

    // create query string as key
    StringBuilder queryString = new StringBuilder();
    for (Map.Entry<String, String> entry : queryData.entrySet()) {
        try {
            String encodedName = URLEncoder.encode(entry.getKey(), "UTF-8");
            Object value = entry.getValue();
            String encodedValue = value != null ? URLEncoder.encode(String.valueOf(value), "UTF-8") : "";
            // do not include page=1
            if (encodedName.equals("page") && encodedValue.equals("1"))
                continue;
            if (queryString.length() > 0)
                queryString.append('&');
            queryString.append(encodedName).append('=').append(encodedValue);
        } catch (Exception e) {
            logger.warn("Could not encode field " + entry.getKey() + " with value " + entry.getValue(), e);
        }
    }

    // get session id - make session dependent
    Identity identity = injector.getInstance(Identity.class);
    String id;
    // not logged in/no session
    if (identity == null || !identity.isAuthenticated() || identity.getId() == null)
        id = "NOTLOGGEDIN";
    // admin view? all admins get the same view
    else if (identity.hasRole("ADMIN"))
        id = "ADMIN";
    // single user cache - might have access to certain elements only
    else
        id = identity.getId();

    // create key
    return httpRequest.getMethod() + language + urlPart + id + encode(queryString);
}