Example usage for java.net URL getQuery

List of usage examples for java.net URL getQuery

Introduction

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

Prototype

public String getQuery() 

Source Link

Document

Gets the query part of this URL .

Usage

From source file:biz.neustar.nexus.plugins.gitlab.GitlabAuthenticatingRealmIT.java

License:asdf

@Test
public void testPlugin() throws Exception {
    assertTrue(nexus().isRunning());/*from   ww w  . j a  va  2  s  . co m*/

    URL nexusUrl = nexus().getUrl();
    URI uri = new URIBuilder().setHost(nexusUrl.getHost()).setPath(nexusUrl.getPath())
            .setPort(nexusUrl.getPort()).setParameters(URLEncodedUtils.parse(nexusUrl.getQuery(), UTF_8))
            .setScheme(nexusUrl.getProtocol()).setUserInfo("jdamick", "asdfasdfasdf").build()
            .resolve("content/groups/public/");

    HttpClient httpclient = HttpClientBuilder.create().build();

    {// request 1
        HttpGet req1 = new HttpGet(uri);
        HttpResponse resp1 = httpclient.execute(req1);
        assertEquals(200, resp1.getStatusLine().getStatusCode());

        RecordedRequest request = server.takeRequest(); // 1 request recorded
        assertEquals("/api/v3/session", request.getPath());
        req1.releaseConnection();
    }

    // failure checks
    { // request 2
        HttpGet req2 = new HttpGet(uri);
        HttpResponse resp2 = httpclient.execute(req2);
        assertEquals(401, resp2.getStatusLine().getStatusCode());
        req2.releaseConnection();
    }

    { // request 3
        HttpGet req3 = new HttpGet(uri);
        HttpResponse resp3 = httpclient.execute(req3);
        assertEquals(401, resp3.getStatusLine().getStatusCode());
        req3.releaseConnection();
    }
}

From source file:edu.uci.ics.crawler4j.url.URLCanonicalizer.java

public static String getCanonicalURL(String href, String context) {

    try {/*w w w.  j ava 2 s.c  o  m*/
        URL canonicalURL = new URL(UrlResolver.resolveUrl(context == null ? "" : context, href));

        String host = canonicalURL.getHost().toLowerCase();
        if (StringUtils.isBlank(host)) {
            // This is an invalid Url.
            return null;
        }

        String path = canonicalURL.getPath();

        /*
         * Normalize: no empty segments (i.e., "//"), no segments equal to
         * ".", and no segments equal to ".." that are preceded by a segment
         * not equal to "..".
         */
        path = new URI(path).normalize().toString();

        /*
         * Convert '//' -> '/'
         */
        int idx = path.indexOf("//");
        while (idx >= 0) {
            path = path.replace("//", "/");
            idx = path.indexOf("//");
        }

        /*
         * Drop starting '/../'
         */
        while (path.startsWith("/../")) {
            path = path.substring(3);
        }

        /*
         * Trim
         */
        path = path.trim();

        final SortedMap<String, String> params = createParameterMap(canonicalURL.getQuery());
        final String queryString;

        if (params != null && params.size() > 0) {
            String canonicalParams = canonicalize(params);
            queryString = (canonicalParams.isEmpty() ? "" : "?" + canonicalParams);
        } else {
            queryString = "";
        }

        /*
         * Add starting slash if needed
         */
        if (path.length() == 0) {
            path = "/" + path;
        }

        /*
         * Drop default port: example.com:80 -> example.com
         */
        int port = canonicalURL.getPort();
        if (port == canonicalURL.getDefaultPort()) {
            port = -1;
        }

        String protocol = canonicalURL.getProtocol().toLowerCase();
        String pathAndQueryString = normalizePath(path) + queryString;

        URL result = new URL(protocol, host, port, pathAndQueryString);
        return result.toExternalForm();

    } catch (MalformedURLException ex) {
        return null;
    } catch (URISyntaxException ex) {
        return null;
    }
}

From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java

/**
 * Return the root node of a schema tree.
 * // w  w w.  ja v a 2 s.  c  o  m
 * @param fileName
 * @return
 * @throws OdaException
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
public static ATreeNode getSchemaTree(String fileName, boolean incAttr)
        throws OdaException, MalformedURLException, URISyntaxException {
    includeAttribute = incAttr;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    URI uri = null;
    File f = new File(fileName);
    if (f.exists()) {
        uri = f.toURI();
    } else {
        URL url = new URL(fileName);
        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    }

    // Then try to parse the input string as a url in web.
    if (uri == null) {
        uri = new URI(fileName);
    }

    // fixed a bug when parse one file contians Franch ,maybe need modification
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(uri.toString());
    if (xsModel == null) {
        try {
            Grammar loadGrammar = xsLoader.loadGrammar(
                    new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1"));
            xsModel = ((XSGrammar) loadGrammar).toXSModel();
        } catch (XNIException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ATreeNode complexTypesRoot = populateComplexTypeTree(xsModel);

    XSNamedMap map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);

    ATreeNode root = new ATreeNode();

    root.setValue("ROOT");
    for (int i = 0; i < map.getLength(); i++) {
        ATreeNode node = new ATreeNode();
        XSElementDecl element = (XSElementDecl) map.item(i);

        String namespace = element.getNamespace();
        XSObject unique = element.getIdentityConstraints().itemByName(namespace, element.getName());
        if (unique instanceof UniqueOrKey) {
            node.getUniqueNames().clear();
            UniqueOrKey uniqueOrKey = (UniqueOrKey) unique;
            String uniqueName = "";
            StringList fieldStrs = uniqueOrKey.getFieldStrs();
            for (int j = 0; j < fieldStrs.getLength(); j++) {
                uniqueName = fieldStrs.item(j);
                if (uniqueName != null && !"".equals(uniqueName)) {
                    uniqueName = uniqueName.replace("/", "").replace(".", "");
                    node.getUniqueNames().add(uniqueName);
                }
            }
        }
        ATreeNode namespaceNode = null;
        if (namespace != null) {
            namespaceNode = new ATreeNode();
            namespaceNode.setDataType(namespace);
            namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
            namespaceNode.setValue(namespace);
        }

        node.setValue(element.getName());
        node.setType(ATreeNode.ELEMENT_TYPE);
        node.setDataType(element.getName());
        if (element.getTypeDefinition() instanceof XSComplexTypeDecl) {
            XSComplexTypeDecl complexType = (XSComplexTypeDecl) element.getTypeDefinition();
            // If the complex type is explicitly defined, that is, it has name.
            if (complexType.getName() != null) {
                node.setDataType(complexType.getName());
                ATreeNode n = findComplexElement(complexTypesRoot, complexType.getName());
                if (namespaceNode != null) {
                    node.addChild(namespaceNode);
                }
                if (n != null) {
                    node.addChild(n.getChildren());
                }
            }
            // If the complex type is implicitly defined, that is, it has no name.
            else {

                addParticleAndAttributeInfo(node, complexType, complexTypesRoot, new VisitingRecorder());
            }
        }
        root.addChild(node);
    }

    // if no base element, display all complex types / attributes directly.
    if (map.getLength() == 0) {
        root.addChild(complexTypesRoot.getChildren());
    }

    populateRoot(root);
    return root;

}

From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java

/**
 * Return the root node of a schema tree.
 * //from  w  w  w . j a v  a 2 s. co  m
 * @param fileName
 * @return
 * @throws OdaException
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
public static ATreeNode getSchemaTree(String fileName, boolean incAttr, boolean forMDM, List<String> attList)
        throws OdaException, MalformedURLException, URISyntaxException {
    includeAttribute = incAttr;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    URI uri = null;
    File f = new File(fileName);
    if (f.exists()) {
        uri = f.toURI();
    } else {
        URL url = new URL(fileName);
        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    }

    // Then try to parse the input string as a url in web.
    if (uri == null) {
        uri = new URI(fileName);
    }

    // fixed a bug when parse one file contians Franch ,maybe need modification
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(uri.toString());
    if (xsModel == null) {
        try {
            Grammar loadGrammar = xsLoader.loadGrammar(
                    new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1"));
            xsModel = ((XSGrammar) loadGrammar).toXSModel();
        } catch (XNIException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ATreeNode complexTypesRoot = populateComplexTypeTree(xsModel);

    XSNamedMap map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);

    ATreeNode root = new ATreeNode();
    root.setValue("ROOT");
    for (int i = 0; i < map.getLength(); i++) {
        ATreeNode node = new ATreeNode();
        XSElementDecl element = (XSElementDecl) map.item(i);

        String namespace = element.getNamespace();
        XSObject unique = element.getIdentityConstraints().itemByName(namespace, element.getName());
        if (unique instanceof UniqueOrKey) {
            node.getUniqueNames().clear();
            UniqueOrKey uniqueOrKey = (UniqueOrKey) unique;
            String uniqueName = "";
            StringList fieldStrs = uniqueOrKey.getFieldStrs();
            for (int j = 0; j < fieldStrs.getLength(); j++) {
                uniqueName = fieldStrs.item(j);
                if (uniqueName != null && !"".equals(uniqueName)) {
                    uniqueName = uniqueName.replace("/", "").replace(".", "");
                    node.getUniqueNames().add(uniqueName);
                }
            }
        }
        ATreeNode namespaceNode = null;
        if (namespace != null) {
            namespaceNode = new ATreeNode();
            namespaceNode.setDataType(namespace);
            namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
            namespaceNode.setValue(namespace);
        }

        node.setValue(element.getName());
        node.setType(ATreeNode.ELEMENT_TYPE);
        node.setDataType(element.getName());

        if (element.getTypeDefinition() instanceof XSComplexTypeDecl) {
            XSComplexTypeDecl complexType = (XSComplexTypeDecl) element.getTypeDefinition();
            // If the complex type is explicitly defined, that is, it has name.
            if (complexType.getName() != null) {
                node.setDataType(complexType.getName());
                ATreeNode n = findComplexElement(complexTypesRoot, complexType.getName());
                if (namespaceNode != null) {
                    node.addChild(namespaceNode);
                }
                if (n != null) {
                    node.addChild(n.getChildren());
                }
            }
            // If the complex type is implicitly defined, that is, it has no name.
            else {
                // If the complex type is implicitly defined , no need to check it in explicitly complex type
                addParticleAndAttributeInfo(node, complexType, complexTypesRoot, new VisitingRecorder());
            }
        }
        String nodeType = node.getOriginalDataType();
        if (forMDM) {
            Object nodeValue = node.getValue();
            if (nodeValue != null && !attList.contains(nodeValue) && nodeType != null
                    && !attList.contains(nodeType)) {
                continue;
            }
        } else {
            if (nodeType != null && !attList.contains(nodeType)) {
                continue;
            }
        }
        if (forMDM && attList.size() == 1 && node.getValue().equals(attList.get(0))) {
            root = node;
        } else {
            root.addChild(node);
        }
    }

    // if no base element, display all complex types / attributes directly.
    if (map.getLength() == 0) {
        root.addChild(complexTypesRoot.getChildren());
    }

    populateRoot(root);
    return root;

}

From source file:com.snowplowanalytics.refererparser.Parser.java

public Referer parse(URL refererUrl, String pageHost) {
    if (refererUrl == null) {
        return null;
    }/*  w  w w .  ja va2 s .  c om*/
    return parse(refererUrl.getProtocol(), refererUrl.getHost(), refererUrl.getPath(), refererUrl.getQuery(),
            pageHost);
}

From source file:com.squid.kraken.v4.auth.OAuth2LoginServlet.java

/**
 * @throws UnsupportedEncodingException/*ww  w .jav  a  2s.co m*/
 * @throws MalformedURLException
 *
 */
protected boolean isSso(HttpServletRequest request, String redirectUri)
        throws UnsupportedEncodingException, MalformedURLException {
    boolean isSso = false;
    URL url = new URL(redirectUri);
    Map<String, List<String>> parameters = getRedirectParameters(url.getQuery());
    if (parameters != null && parameters.containsKey(SSO)) {
        isSso = true;
    }
    return isSso;
    // getQueryPairs(parameters)
}

From source file:net.technicpack.launchercore.mirror.MirrorStore.java

private URL addDownloadKey(URL url, String downloadHost, String downloadKey, String clientId) {
    if (downloadHost != null && !downloadHost.isEmpty()) {
        try {//from  w ww  .  j a  v a 2  s.  c  o  m
            url = new URI(url.getProtocol(), url.getUserInfo(), downloadHost, url.getPort(), url.getPath(),
                    url.getQuery(), null).toURL();
        } catch (URISyntaxException ex) {
            //Ignore, just keep old url
        } catch (MalformedURLException ex) {
            //Ignore, just keep old url
        }
    }

    String textUrl = url.toString();

    if (url.getQuery() == null || url.getQuery().isEmpty()) {
        textUrl += "?";
    } else {
        textUrl += "&";
    }

    textUrl += "t=" + downloadKey + "&c=" + clientId;

    try {
        return new URL(textUrl);
    } catch (MalformedURLException ex) {
        throw new Error("Code error: managed to take valid url " + url.toString()
                + " and turn it into invalid URL " + textUrl);
    }
}

From source file:com.wso2telco.dep.mediator.impl.smsmessaging.RetrieveSMSHandler.java

@Override
public boolean handle(MessageContext context) throws CustomException, AxisFault, Exception {

    SOAPBody body = context.getEnvelope().getBody();
    Gson gson = new GsonBuilder().serializeNulls().create();

    String reqType = "retrive_sms";
    String requestid = UID.getUniqueID(Type.SMSRETRIVE.getCode(), context, executor.getApplicationid());
    // String appID = apiUtil.getAppID(context, reqType);

    int batchSize = 100;

    URL retrieveURL = new URL("http://example.com/smsmessaging/v1" + executor.getSubResourcePath());
    String urlQuery = retrieveURL.getQuery();

    if (urlQuery != null) {
        if (urlQuery.contains("maxBatchSize")) {
            String queryParts[] = urlQuery.split("=");
            if (queryParts.length > 1) {
                if (Integer.parseInt(queryParts[1]) < 100) {
                    batchSize = Integer.parseInt(queryParts[1]);
                }/*  w w  w .j a v a 2s.co  m*/
            }
        }

        List<OperatorEndpoint> endpoints = occi.getAPIEndpointsByApp(API_TYPE, executor.getSubResourcePath(),
                executor.getValidoperators());

        log.info("Endpoints size: " + endpoints.size());

        Collections.shuffle(endpoints);

        int perOpCoLimit = batchSize / (endpoints.size());

        log.info("Per OpCo limit :" + perOpCoLimit);

        JSONArray results = new JSONArray();

        int execCount = 0;
        int forLoopCount = 0;
        boolean retryFlag = true;
        FileReader fileReader = new FileReader();
        String file = CarbonUtils.getCarbonConfigDirPath() + File.separator
                + FileNames.MEDIATOR_CONF_FILE.getFileName();
        Map<String, String> mediatorConfMap = fileReader.readPropertyFile(file);
        Boolean retry = false;
        retry = Boolean.valueOf(mediatorConfMap.get("retry_on_fail"));
        Integer retryCount = Integer.valueOf(mediatorConfMap.get("retry_count"));

        ArrayList<String> responses = new ArrayList<String>();
        while ((results.length() < batchSize) && (retryFlag == true)) {
            execCount++;
            log.info("Default aEndpoint : " + endpoints.size());
            for (int i = 0; i < endpoints.size(); i++) {
                forLoopCount++;
                log.info("Default forLoopCount : " + forLoopCount);
                OperatorEndpoint aEndpoint = endpoints.remove(0);
                log.info("Default aEndpoint : " + aEndpoint.getEndpointref().getAddress());
                endpoints.add(aEndpoint);
                String url = aEndpoint.getEndpointref().getAddress();
                APICall ac = apiUtil.setBatchSize(url, body.toString(), reqType, perOpCoLimit);

                JSONObject obj = ac.getBody();
                String retStr = null;
                log.info("Retrieving messages of operator: " + aEndpoint.getOperator());

                if (context.isDoingGET()) {
                    log.info("Doing makeGetRequest");
                    retStr = executor.makeRetrieveSMSGetRequest(aEndpoint, ac.getUri(), null, true, context,
                            false);
                } else {
                    log.info("Doing makeRequest");
                    retStr = executor.makeRequest(aEndpoint, ac.getUri(), obj.toString(), true, context, false);
                }
                log.info("Retrieved messages of " + aEndpoint.getOperator() + " operator: " + retStr);

                if (retStr != null) {

                    JSONArray resList = apiUtil.getResults(reqType, retStr);
                    if (resList != null) {
                        for (int t = 0; t < resList.length(); t++) {
                            results.put(resList.get(t));
                        }
                        responses.add(retStr);
                    }

                }

                if (results.length() >= batchSize) {
                    break;
                }
            }

            if (retry == false) {
                retryFlag = false;
                log.info("11 Final value of retryFlag :" + retryFlag);
            }

            if (execCount >= retryCount) {
                retryFlag = false;
                log.info("22 Final value of retryFlag :" + retryFlag);
            }

            log.info("Final value of count :" + execCount);
            log.info("Results length of retrieve messages: " + results.length());

        }

        JSONObject paylodObject = apiUtil.generateResponse(context, reqType, results, responses, requestid);
        String strjsonBody = paylodObject.toString();

        /*add resourceURL to the southbound response*/
        SouthboundRetrieveResponse sbRetrieveResponse = gson.fromJson(strjsonBody,
                SouthboundRetrieveResponse.class);
        if (sbRetrieveResponse != null && sbRetrieveResponse.getInboundSMSMessageList() != null) {
            String resourceURL = sbRetrieveResponse.getInboundSMSMessageList().getResourceURL();
            InboundSMSMessage[] inboundSMSMessageResponses = sbRetrieveResponse.getInboundSMSMessageList()
                    .getInboundSMSMessage();

            for (int i = 0; i < inboundSMSMessageResponses.length; i++) {
                String messageId = inboundSMSMessageResponses[i].getMessageId();
                inboundSMSMessageResponses[i].setResourceURL(resourceURL + "/" + messageId);
            }
            sbRetrieveResponse.getInboundSMSMessageList().setInboundSMSMessage(inboundSMSMessageResponses);
        }

        executor.removeHeaders(context);
        executor.setResponse(context, gson.toJson(sbRetrieveResponse));
        // routeToEndPoint(context, sendResponse, "", "", strjsonBody);

        ((Axis2MessageContext) context).getAxis2MessageContext().setProperty("messageType", "application/json");

    }
    return true;
}

From source file:com.comcast.cns.io.HTTPEndpointAsyncPublisher.java

@Override
public void send() throws Exception {

    HttpAsyncRequester requester = new HttpAsyncRequester(httpProcessor, new DefaultConnectionReuseStrategy(),
            httpParams);//from   w  w  w .  j a  va 2s.  c  o  m
    final URL url = new URL(endpoint);
    final HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());

    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
            url.getPath() + (url.getQuery() == null ? "" : "?" + url.getQuery()));
    composeHeader(request);

    String msg = null;

    if (message.getMessageStructure() == CNSMessageStructure.json) {
        msg = message.getProtocolSpecificMessage(CnsSubscriptionProtocol.http);
    } else {
        msg = message.getMessage();
    }

    if (!rawMessageDelivery && message.getMessageType() == CNSMessageType.Notification) {
        msg = com.comcast.cns.util.Util.generateMessageJson(message, CnsSubscriptionProtocol.http);
    }

    logger.debug("event=send_async_http_request endpoint=" + endpoint + "\" message=\"" + msg + "\"");

    request.setEntity(new NStringEntity(msg));

    requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(),
            connectionPool, new BasicHttpContext(), new FutureCallback<HttpResponse>() {

                public void completed(final HttpResponse response) {

                    int statusCode = response.getStatusLine().getStatusCode();

                    // accept all 2xx status codes

                    if (statusCode >= 200 && statusCode < 300) {
                        callback.onSuccess();
                    } else {
                        logger.warn(target + "://" + url.getPath() + "?" + url.getQuery() + " -> "
                                + response.getStatusLine());
                        callback.onFailure(statusCode);
                    }
                }

                public void failed(final Exception ex) {
                    logger.warn(target + " " + url.getPath() + " " + url.getQuery(), ex);
                    callback.onFailure(0);
                }

                public void cancelled() {
                    logger.warn(target + " " + url.getPath() + " " + url.getQuery() + " -> " + "cancelled");
                    callback.onFailure(1);
                }
            });
}

From source file:org.fao.geonet.utils.GeonetHttpRequestFactory.java

/**
 * Ceate an XmlRequest from a url.//from  w  w  w.  j ava2s.  co  m
 *
 * @param url the url of the request.
 * @return the XmlRequest.
 */
public final XmlRequest createXmlRequest(URL url) {
    final int port = url.getPort();
    final XmlRequest request = createXmlRequest(url.getHost(), port, url.getProtocol());

    request.setAddress(url.getPath());
    request.setQuery(url.getQuery());
    request.setFragment(url.getRef());
    request.setUserInfo(url.getUserInfo());
    request.setCookieStore(new BasicCookieStore());
    return request;
}