Example usage for java.net URI getQuery

List of usage examples for java.net URI getQuery

Introduction

In this page you can find the example usage for java.net URI getQuery.

Prototype

public String getQuery() 

Source Link

Document

Returns the decoded query component of this URI.

Usage

From source file:org.pentaho.big.data.kettle.plugins.hdfs.trans.HadoopFileInputMeta.java

protected String encryptDecryptPassword(String source, EncryptDirection direction) {
    Validate.notNull(direction, "'direction' must not be null");
    try {//from w  ww. j ava2 s .  c  o  m
        URI uri = new URI(source);
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            String[] userInfoArray = userInfo.split(":", 2);
            if (userInfoArray.length < 2) {
                return source; //no password present
            }
            String password = userInfoArray[1];
            String processedPassword = password;
            switch (direction) {
            case ENCRYPT:
                processedPassword = Encr.encryptPasswordIfNotUsingVariables(password);
                break;
            case DECRYPT:
                processedPassword = Encr.decryptPasswordOptionallyEncrypted(password);
                break;
            default:
                throw new InvalidParameterException("direction must be 'ENCODE' or 'DECODE'");
            }
            URI encryptedUri = new URI(uri.getScheme(), userInfoArray[0] + ":" + processedPassword,
                    uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
            return encryptedUri.toString();
        }
    } catch (URISyntaxException e) {
        return source; // if this is non-parseable as a uri just return the source without changing it.
    }
    return source; // Just for the compiler should NEVER hit this code
}

From source file:org.apache.solr.update.InvenioKeepRecidUpdated.java

private void runProcessing(SolrCore core, String handlerUrl, int[] recids, SolrQueryRequest req)
        throws MalformedURLException, IOException, InterruptedException {

    URI u = null;
    SolrRequestHandler handler = null;//  ww w .j a va 2s  .  com
    try {
        u = new URI(handlerUrl);
        String p = u.getPath();
        if (u.getHost() == null || u.getHost() == "") {
            if (core.getRequestHandler(p) != null) {
                handler = core.getRequestHandler(p);
            } else if (!p.startsWith("/") && core.getRequestHandler("/" + p) != null) {
                handler = core.getRequestHandler("/" + p);
            }

            if (handler != null) {
                Map<String, List<String>> handlerParams = WebUtils.parseQuery(u.getQuery());
                HashMap<String, String[]> hParams = new HashMap<String, String[]>();
                for (String val : handlerParams.keySet()) {
                    String[] nV = new String[handlerParams.get(val).size()];
                    int i = 0;
                    for (String v : handlerParams.get(val)) {
                        nV[i] = v;
                        i++;
                    }
                    hParams.put(val, nV);
                }

                runProcessingInternally(handler, recids, req, hParams);
                return;
            }
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    runProcessingUpload(handlerUrl, recids, req);
}

From source file:org.kie.commons.java.nio.fs.jgit.JGitFileSystemProvider.java

private boolean hasForceFlag(URI uri) {
    checkNotNull("uri", uri);

    if (uri.getQuery() != null) {
        return uri.getQuery().contains("force");
    }/*w  ww .j  a v  a2  s.  c o m*/

    return false;
}

From source file:org.kie.commons.java.nio.fs.jgit.JGitFileSystemProvider.java

private boolean hasSyncFlag(final URI uri) {
    checkNotNull("uri", uri);

    if (uri.getQuery() != null) {
        return uri.getQuery().contains("sync");
    }//from   ww  w.ja  v a  2s . com

    return false;
}

From source file:com.ning.http.client.providers.NettyAsyncHttpProvider.java

Url createUrl(String u) {
    URI uri = URI.create(u);
    final String scheme = uri.getScheme();
    if (scheme == null || !scheme.equalsIgnoreCase("http")) {
        throw new IllegalArgumentException(
                "The URI scheme, of the URI " + u + ", must be equal (ignoring case) to 'http'");
    }//from w w  w . j a va  2s .  c  o  m

    String path = uri.getPath();
    if (path == null) {
        throw new IllegalArgumentException("The URI path, of the URI " + uri + ", must be non-null");
    } else if (path.length() == 0) {
        throw new IllegalArgumentException("The URI path, of the URI " + uri + ", must be present");
    } else if (path.charAt(0) != '/') {
        throw new IllegalArgumentException("The URI path, of the URI " + uri + ". must start with a '/'");
    }

    int port = (uri.getPort() == -1) ? 80 : uri.getPort();
    return new Url(uri.getScheme(), uri.getHost(), port, uri.getPath(), uri.getQuery());
}

From source file:org.mapfish.print.PDFUtils.java

private static Image loadImageFromUrl(final RenderingContext context, final URI uri,
        final boolean alwaysThrowExceptionOnError) throws IOException, DocumentException {
    if (!uri.isAbsolute()) {
        //Assumption is that the file is on the local file system
        return Image.getInstance(uri.toString());
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        String path;/*from ww  w.  j ava2s  . c o  m*/
        if (uri.getHost() != null && uri.getPath() != null) {
            path = uri.getHost() + uri.getPath();
        } else if (uri.getHost() == null && uri.getPath() != null) {
            path = uri.getPath();
        } else {
            path = uri.toString().substring("file:".length()).replaceAll("/+", "/");
        }
        path = path.replace("/", File.separator);
        return Image.getInstance(new File(path).toURI().toURL());
    } else {

        final String contentType;
        final int statusCode;
        final String statusText;
        byte[] data = null;
        try {
            //read the whole image content in memory, then give that to iText
            if ((uri.getScheme().equals("http") || uri.getScheme().equals("https"))
                    && context.getConfig().localHostForwardIsFrom(uri.getHost())) {
                String scheme = uri.getScheme();
                final String host = uri.getHost();
                if (uri.getScheme().equals("https") && context.getConfig().localHostForwardIsHttps2http()) {
                    scheme = "http";
                }
                URL url = new URL(scheme, "localhost", uri.getPort(), uri.getPath() + "?" + uri.getQuery());

                HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
                connexion.setRequestProperty("Host", host);
                for (Map.Entry<String, String> entry : context.getHeaders().entrySet()) {
                    connexion.setRequestProperty(entry.getKey(), entry.getValue());
                }
                InputStream is = null;
                try {
                    try {
                        is = connexion.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = is.read(buffer)) != -1) {
                            baos.write(buffer, 0, length);
                        }
                        baos.flush();
                        data = baos.toByteArray();
                    } catch (IOException e) {
                        LOGGER.warn(e);
                    }
                    statusCode = connexion.getResponseCode();
                    statusText = connexion.getResponseMessage();
                    contentType = connexion.getContentType();
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            } else {
                GetMethod getMethod = null;
                try {
                    getMethod = new GetMethod(uri.toString());
                    for (Map.Entry<String, String> entry : context.getHeaders().entrySet()) {
                        getMethod.setRequestHeader(entry.getKey(), entry.getValue());
                    }
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("loading image: " + uri);
                    context.getConfig().getHttpClient(uri).executeMethod(getMethod);
                    statusCode = getMethod.getStatusCode();
                    statusText = getMethod.getStatusText();

                    Header contentTypeHeader = getMethod.getResponseHeader("Content-Type");
                    if (contentTypeHeader == null) {
                        contentType = "";
                    } else {
                        contentType = contentTypeHeader.getValue();
                    }
                    data = getMethod.getResponseBody();
                } finally {
                    if (getMethod != null) {
                        getMethod.releaseConnection();
                    }
                }
            }

            if (statusCode == 204) {
                // returns a transparent image
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("creating a transparent image for: " + uri);
                try {
                    final byte[] maskr = { (byte) 255 };
                    Image mask = Image.getInstance(1, 1, 1, 1, maskr);
                    mask.makeMask();
                    data = new byte[1 * 1 * 3];
                    Image image = Image.getInstance(1, 1, 3, 8, data);
                    image.setImageMask(mask);
                    return image;
                } catch (DocumentException e) {
                    LOGGER.warn("Couldn't generate a transparent image");

                    if (alwaysThrowExceptionOnError) {
                        throw e;
                    }

                    Image image = handleImageLoadError(context, e.getMessage());

                    LOGGER.warn("The status code was not a valid code, a default image is being returned.");

                    return image;
                }
            } else if (statusCode < 200 || statusCode >= 300 || contentType.startsWith("text/")
                    || contentType.equals("application/vnd" + ".ogc.se_xml")) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Server returned an error for " + uri + ": " + new String(data));
                String errorMessage;
                if (statusCode < 200 || statusCode >= 300) {
                    errorMessage = "Error (status=" + statusCode + ") while reading the image from " + uri
                            + ": " + statusText;
                } else {
                    errorMessage = "Didn't receive an image while reading: " + uri;
                }

                if (alwaysThrowExceptionOnError) {
                    throw new IOException(errorMessage);
                }

                Image image = handleImageLoadError(context, errorMessage);

                LOGGER.warn("The status code was not a valid code, a default image is being returned.");

                return image;
            } else {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("loaded image: " + uri);
                return Image.getInstance(data);
            }
        } catch (IOException e) {
            LOGGER.error("Server returned an error for " + uri + ": " + e.getMessage());

            if (alwaysThrowExceptionOnError) {
                throw e;
            }

            return handleImageLoadError(context, e.getMessage());
        }
    }
}

From source file:tajo.master.GlobalPlanner.java

private Map<TupleRange, Set<URI>> rangeFetches(Schema schema, List<URI> uriList)
        throws UnsupportedEncodingException {
    SortedMap<TupleRange, Set<URI>> map = Maps.newTreeMap();
    TupleRange range;//from   w  w w  . j  a  va 2s .  co m
    Set<URI> uris;
    for (URI uri : uriList) {
        range = TupleUtil.queryToRange(schema, uri.getQuery()); // URI.getQuery() returns a url-decoded query string.
        if (map.containsKey(range)) {
            uris = map.get(range);
            uris.add(uri);
        } else {
            uris = Sets.newHashSet();
            uris.add(uri);
            map.put(range, uris);
        }
    }

    return map;
}

From source file:com.tesora.dve.common.PEUrl.java

private PEUrl parseURL(String urlString) throws PEException {

    try {/*from   w  ww  .j  a va2 s.  c o  m*/
        URI parser = new URI(urlString.trim());
        final String protocol = parser.getScheme();

        parser = URI.create(parser.getSchemeSpecificPart());
        final String subProtocol = parser.getScheme();
        final String authority = parser.getAuthority();

        if ((protocol == null) || (subProtocol == null)) {
            throw new PEException("Malformed URL '" + urlString + "' - incomplete protocol");
        } else if (authority == null) {
            throw new PEException("Malformed URL '" + urlString + "' - invalid authority");
        }

        this.setProtocol(protocol);
        this.setSubProtocol(subProtocol);

        final String host = parser.getHost();
        if (host != null) {
            this.setHost(host);
        }

        final int port = parser.getPort();
        if (port != -1) {
            this.setPort(port);
        }

        final String query = parser.getQuery();
        if (query != null) {
            this.setQuery(query);
        }

        final String path = parser.getPath();
        if ((path != null) && !path.isEmpty()) {
            this.setPath(path.startsWith("/") ? path.substring(1) : path);
        }

        return this;
    } catch (final URISyntaxException | IllegalArgumentException e) {
        throw new PEException("Malformed URL '" + urlString + "'", e);
    }
}

From source file:com.snowplowanalytics.snowplow.hadoop.hive.SnowPlowEventStruct.java

/**
 * Parses the input row String into a Java object.
 * For performance reasons this works in-place updating the fields
 * within this SnowPlowEventStruct, rather than creating a new one.
 *
 * @param row The raw String containing the row contents
 * @return true if row was successfully parsed, false otherwise
 * @throws SerDeException For any exception during parsing
 *///w  ww  .j a va2s.c  o  m
public boolean updateByParsing(String row) throws SerDeException {

    // First we reset the object's fields
    nullify();

    // We have to handle any header rows
    if (row.startsWith("#Version:") || row.startsWith("#Fields:")) {
        return false; // Empty row will be discarded by Hive
    }

    final Matcher m = cfRegex.matcher(row);

    // 0. First check our row is kosher

    // -> is row from a CloudFront log? Will throw an exception if not
    if (!m.matches())
        throw new SerDeException("Row does not match expected CloudFront regexp pattern");

    // -> was generated by sp.js? If not (e.g. favicon.ico request), then silently return
    final String object = m.group(8);
    final String querystring = m.group(12);
    if (!(object.startsWith("/ice.png") || object.equals("/i") || object.startsWith("/i?"))
            || isNullField(querystring)) { // Also works if Forward Query String = yes
        return false;
    }

    // 1. Now we retrieve the fields which get directly passed through
    this.dt = m.group(1);
    this.collector_dt = this.dt;
    this.collector_tm = m.group(2); // CloudFront date format matches Hive's
    this.user_ipaddress = m.group(5);

    // 2. Now grab the user agent
    final String ua = m.group(11);
    try {
        this.useragent = decodeSafeString(ua);
    } catch (Exception e) {
        getLog().warn(e.getClass().getSimpleName() + " on { useragent: " + ua + " }");
    }

    // 3. Next we dis-assemble the user agent...
    final UserAgent userAgent = UserAgent.parseUserAgentString(ua);

    // -> browser fields
    final Browser b = userAgent.getBrowser();
    this.br_name = b.getName();
    this.br_family = b.getGroup().getName();
    final Version v = userAgent.getBrowserVersion();
    this.br_version = (v == null) ? null : v.getVersion();
    this.br_type = b.getBrowserType().getName();
    this.br_renderengine = b.getRenderingEngine().toString();

    // -> OS-related fields
    final OperatingSystem os = userAgent.getOperatingSystem();
    this.os_name = os.getName();
    this.os_family = os.getGroup().getName();
    this.os_manufacturer = os.getManufacturer().getName();

    // -> device/hardware-related fields
    this.dvce_type = os.getDeviceType().getName();
    this.dvce_ismobile = os.isMobileDevice();
    this.dvce_ismobile_bt = (byte) (this.dvce_ismobile ? 1 : 0);

    // 4. Now for the versioning (tracker versioning is handled below)
    this.v_collector = collectorVersion;
    this.v_etl = "serde-" + ProjectSettings.VERSION;

    // 5. Now we generate the event vendor and ID
    this.event_vendor = eventVendor; // TODO: this becomes part of the protocol eventually
    this.event_id = generateEventId();

    // 6. Now we dis-assemble the querystring.
    String qsUrl = null; // We use this in the block below, and afterwards
    List<NameValuePair> params = null; // We re-use this for efficiency
    try {
        params = URLEncodedUtils.parse(URI.create("http://localhost/?" + querystring), cfEncoding);

        // For performance, don't convert to a map, just loop through and match to our variables as we go
        for (NameValuePair pair : params) {

            final String name = pair.getName();
            final String value = pair.getValue();

            final QuerystringFields field;
            try {
                field = QuerystringFields.valueOf(name.toUpperCase()); // Java pre-7 can't switch on a string, so hash the string
            } catch (IllegalArgumentException e) {
                getLog().warn("Unexpected params { " + name + ": " + value + " }");
                continue;
            }

            try {
                switch (field) {
                // Common fields
                case E:
                    this.event = asEventType(value);
                    break;
                case IP:
                    this.user_ipaddress = value;
                    break;
                case AID:
                    this.app_id = value;
                    break;
                case P:
                    this.platform = value;
                    break;
                case TID:
                    this.txn_id = value;
                    break;
                case UID:
                    this.user_id = value;
                    break;
                case DUID:
                    this.domain_userid = value;
                    break;
                case NUID:
                    this.network_userid = value;
                    break;
                case FP:
                    this.user_fingerprint = value;
                    break;
                case VID:
                    this.domain_sessionidx = Integer.parseInt(value);
                    break;
                case DTM:
                    // Set our dvce_dt, _tm and _epoch fields
                    this.dvce_epoch = Long.parseLong(value);
                    Date deviceDate = new Date(this.dvce_epoch);
                    this.dvce_dt = dateFormat.format(deviceDate);
                    this.dvce_tm = timeFormat.format(deviceDate);
                    break;
                case TV:
                    this.v_tracker = value;
                    break;
                case LANG:
                    this.br_lang = value;
                    break;
                case CS:
                    this.doc_charset = value;
                    break;
                case VP:
                    String[] viewport = value.split("x");
                    if (viewport.length != 2)
                        throw new Exception("Couldn't parse vp field");
                    this.br_viewwidth = Integer.parseInt(viewport[0]);
                    this.br_viewheight = Integer.parseInt(viewport[1]);
                    break;
                case DS:
                    String[] docsize = value.split("x");
                    if (docsize.length != 2)
                        throw new Exception("Couldn't parse ds field");
                    this.doc_width = Integer.parseInt(docsize[0]);
                    this.doc_height = Integer.parseInt(docsize[1]);
                    break;
                case F_PDF:
                    if ((this.br_features_pdf = stringToByte(value)) == 1)
                        this.br_features.add("pdf");
                    break;
                case F_FLA:
                    if ((this.br_features_flash = stringToByte(value)) == 1)
                        this.br_features.add("fla");
                    break;
                case F_JAVA:
                    if ((this.br_features_java = stringToByte(value)) == 1)
                        this.br_features.add("java");
                    break;
                case F_DIR:
                    if ((this.br_features_director = stringToByte(value)) == 1)
                        this.br_features.add("dir");
                    break;
                case F_QT:
                    if ((this.br_features_quicktime = stringToByte(value)) == 1)
                        this.br_features.add("qt");
                    break;
                case F_REALP:
                    if ((this.br_features_realplayer = stringToByte(value)) == 1)
                        this.br_features.add("realp");
                    break;
                case F_WMA:
                    if ((this.br_features_windowsmedia = stringToByte(value)) == 1)
                        this.br_features.add("wma");
                    break;
                case F_GEARS:
                    if ((this.br_features_gears = stringToByte(value)) == 1)
                        this.br_features.add("gears");
                    break;
                case F_AG:
                    if ((this.br_features_silverlight = stringToByte(value)) == 1)
                        this.br_features.add("ag");
                    break;
                case COOKIE:
                    this.br_cookies = stringToBoolean(value);
                    this.br_cookies_bt = (byte) (this.br_cookies ? 1 : 0);
                    break;
                case RES:
                    String[] resolution = value.split("x");
                    if (resolution.length != 2)
                        throw new Exception("Couldn't parse res field");
                    this.dvce_screenwidth = Integer.parseInt(resolution[0]);
                    this.dvce_screenheight = Integer.parseInt(resolution[1]);
                    break;
                case CD:
                    this.br_colordepth = value;
                    break;
                case TZ:
                    this.os_timezone = decodeSafeString(value);
                    break;
                case REFR:
                    this.page_referrer = decodeSafeString(value);
                    break;
                case URL:
                    qsUrl = pair.getValue(); // We might use this later for the page URL
                    break;

                // Page-view only
                case PAGE:
                    this.page_title = decodeSafeString(value);
                    break;

                // Event only
                case EV_CA:
                    this.ev_category = decodeSafeString(value);
                    break;
                case EV_AC:
                    this.ev_action = decodeSafeString(value);
                    break;
                case EV_LA:
                    this.ev_label = decodeSafeString(value);
                    break;
                case EV_PR:
                    this.ev_property = decodeSafeString(value);
                    break;
                case EV_VA:
                    this.ev_value = decodeSafeString(value);
                    break;

                // Ecommerce
                case TR_ID:
                    this.tr_orderid = decodeSafeString(value);
                    break;
                case TR_AF:
                    this.tr_affiliation = decodeSafeString(value);
                    break;
                case TR_TT:
                    this.tr_total = decodeSafeString(value);
                    break;
                case TR_TX:
                    this.tr_tax = decodeSafeString(value);
                    break;
                case TR_SH:
                    this.tr_shipping = decodeSafeString(value);
                    break;
                case TR_CI:
                    this.tr_city = decodeSafeString(value);
                    break;
                case TR_ST:
                    this.tr_state = decodeSafeString(value);
                    break;
                case TR_CO:
                    this.tr_country = decodeSafeString(value);
                    break;
                case TI_ID:
                    this.ti_orderid = decodeSafeString(value);
                    break;
                case TI_SK:
                    this.ti_sku = decodeSafeString(value);
                    break;
                case TI_NA:
                    this.ti_name = decodeSafeString(value);
                    break;
                case TI_CA:
                    this.ti_category = decodeSafeString(value);
                    break;
                case TI_PR:
                    this.ti_price = decodeSafeString(value);
                    break;
                case TI_QU:
                    this.ti_quantity = decodeSafeString(value);
                    break;

                // Page pings
                case PP_MIX:
                    this.pp_xoffset_min = Integer.parseInt(value);
                    break;
                case PP_MAX:
                    this.pp_xoffset_max = Integer.parseInt(value);
                    break;
                case PP_MIY:
                    this.pp_yoffset_min = Integer.parseInt(value);
                    break;
                case PP_MAY:
                    this.pp_yoffset_max = Integer.parseInt(value);
                    break;
                }
            } catch (Exception e) {
                getLog().warn(e.getClass().getSimpleName() + " on { " + name + ": " + value + " }");
            }
        }
    } catch (IllegalArgumentException e) {
        getLog().warn("Corrupted querystring { " + querystring + " }");
    }

    // 7. Choose the page_url
    final String cfUrl = m.group(10);
    if (!isNullField(cfUrl)) { // CloudFront didn't provide the URL as cs(Referer)
        this.page_url = cfUrl; // The CloudFront cs(Referer) URL
    } else {
        try {
            this.page_url = decodeSafeString(qsUrl); // Use the decoded querystring URL. Might be null (returned as null)
        } catch (Exception e) {
            getLog().warn(e.getClass().getSimpleName() + " on { qsUrl: " + qsUrl + " }");
        }
    }

    // 8. Now try to convert the page_url into a valid Java URI and store the 6 out of 9 components we are interested in:
    try {
        final URI pageUri = URI.create(this.page_url);
        this.page_urlscheme = pageUri.getScheme();
        this.page_urlhost = pageUri.getHost();
        final Integer port = pageUri.getPort();
        this.page_urlport = (port == -1) ? 80 : port;
        this.page_urlpath = pageUri.getPath();
        this.page_urlquery = pageUri.getQuery();
        this.page_urlfragment = pageUri.getFragment();
    } catch (Exception e) {
        getLog().warn("Could not parse page_url " + this.page_url + " }");
    }

    // 9. Finally handle the marketing fields in the page_url
    // Re-use params to avoid creating another object
    if (this.page_url != null) {
        params = null;
        try {
            params = URLEncodedUtils.parse(URI.create(this.page_url), "UTF-8");
        } catch (IllegalArgumentException e) {
            getLog().warn("Couldn't parse page_url: " + page_url);
        }
        if (params != null) {
            // For performance, don't convert to a map, just loop through and match to our variables as we go
            for (NameValuePair pair : params) {

                final String name = pair.getName();
                final String value = pair.getValue();

                final MarketingFields field;
                try {
                    field = MarketingFields.valueOf(name.toUpperCase()); // Java pre-7 can't switch on a string, so hash the string
                } catch (IllegalArgumentException e) {
                    // Do nothing: non-marketing related querystring fields are not an issue.
                    continue;
                }

                try {
                    switch (field) {
                    // Marketing fields
                    case UTM_MEDIUM:
                        this.mkt_medium = decodeSafeString(value);
                        break;
                    case UTM_SOURCE:
                        this.mkt_source = decodeSafeString(value);
                        break;
                    case UTM_TERM:
                        this.mkt_term = decodeSafeString(value);
                        break;
                    case UTM_CONTENT:
                        this.mkt_content = decodeSafeString(value);
                        break;
                    case UTM_CAMPAIGN:
                        this.mkt_campaign = decodeSafeString(value);
                        break;
                    }
                } catch (Exception e) {
                    getLog().warn(e.getClass().getSimpleName() + " on { " + name + ": " + value + " }");
                }
            }
        }
    }

    return true; // Successfully updated the row.
}