org.apache.commons.codec.net.URLCodec Java Examples

The following examples show how to use org.apache.commons.codec.net.URLCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataUrlDecoder.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a data URL providing simple access to the information contained by the URL.
 * @param url the string representation of the URL to decode
 * @return the {@link DataUrlDecoder} holding decoded information
 * @throws UnsupportedEncodingException if the encoding specified by the data URL is invalid or not
 * available on the JVM
 * @throws DecoderException if decoding didn't success
 */
public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedEncodingException,
        DecoderException {
    if (!url.startsWith("data")) {
        throw new IllegalArgumentException("Not a data url: " + url);
    }
    final int comma = url.indexOf(',');
    String beforeData = url.substring("data:".length(), comma);

    final boolean base64 = beforeData.endsWith(";base64");
    if (base64) {
        beforeData = beforeData.substring(0, beforeData.length() - 7);
    }
    final String mediaType = extractMediaType(beforeData);
    final Charset charset = extractCharset(beforeData);

    byte[] data = url.substring(comma + 1).getBytes(charset);
    if (base64) {
        data = Base64.decodeBase64(decodeUrl(data));
    }
    else {
        data = URLCodec.decodeUrl(data);
    }

    return new DataUrlDecoder(data, mediaType, charset);
}
 
Example #2
Source File: HttpHelper.java    From SeaCloudsPlatform with Apache License 2.0 6 votes vote down vote up
private String prepareRequestURL(String restPath, List<NameValuePair> params) {
    StringBuilder operationBuilder = new StringBuilder();
    operationBuilder.append(serviceURL);
    operationBuilder.append(restPath);
    if (params.size() > 0) {
        operationBuilder.append("?");
        URLCodec coded = new URLCodec();
        try {
            for (NameValuePair p : params)
                operationBuilder.append(p.getName() + "=" + coded.encode(p.getValue()));

        } catch (EncoderException e) {
            log.error(e.getCause().getMessage(), e);
            return "";
        }
    }
    return operationBuilder.toString();
}
 
Example #3
Source File: DataUrlDecoder.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a data URL providing simple access to the information contained by the URL.
 * @param url the string representation of the URL to decode
 * @return the {@link DataUrlDecoder} holding decoded information
 * @throws UnsupportedEncodingException if the encoding specified by the data URL is invalid or not
 * available on the JVM
 * @throws DecoderException if decoding didn't success
 */
public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedEncodingException,
        DecoderException {
    if (!url.startsWith("data")) {
        throw new IllegalArgumentException("Not a data url: " + url);
    }
    final int comma = url.indexOf(',');
    String beforeData = url.substring("data:".length(), comma);

    final boolean base64 = beforeData.endsWith(";base64");
    if (base64) {
        beforeData = beforeData.substring(0, beforeData.length() - 7);
    }
    final String mediaType = extractMediaType(beforeData);
    final Charset charset = extractCharset(beforeData);

    byte[] data = url.substring(comma + 1).getBytes(charset);
    if (base64) {
        data = Base64.decodeBase64(decodeUrl(data));
    }
    else {
        data = URLCodec.decodeUrl(data);
    }

    return new DataUrlDecoder(data, mediaType, charset);
}
 
Example #4
Source File: UploadId.java    From tus-java-server with MIT License 6 votes vote down vote up
/**
 * Create a new {@link UploadId} instance based on the provided object using it's toString method.
 * @param inputObject The object to use for constructing the ID
 */
public UploadId(Serializable inputObject) {
    String inputValue = (inputObject == null ? null : inputObject.toString());
    Validate.notBlank(inputValue, "The upload ID value cannot be blank");

    this.originalObject = inputObject;
    URLCodec codec = new URLCodec();
    //Check if value is not encoded already
    try {
        if (inputValue != null && inputValue.equals(codec.decode(inputValue, UPLOAD_ID_CHARSET))) {
            this.urlSafeValue = codec.encode(inputValue, UPLOAD_ID_CHARSET);
        } else {
            //value is already encoded, use as is
            this.urlSafeValue = inputValue;
        }
    } catch (DecoderException | UnsupportedEncodingException e) {
        log.warn("Unable to URL encode upload ID value", e);
        this.urlSafeValue = inputValue;
    }
}
 
Example #5
Source File: AzkabanAjaxAPIClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private static void changeProjectDescription(String sessionId, String azkabanServerUrl, String azkabanProjectName,
    String projectDescription)
    throws IOException {
  String encodedProjectDescription;
  try {
    encodedProjectDescription = new URLCodec().encode(projectDescription);
  } catch (EncoderException e) {
    throw new IOException("Could not encode Azkaban project description", e);
  }

  Map<String, String> params = Maps.newHashMap();
  params.put("ajax", "changeDescription");
  params.put("project", azkabanProjectName);
  params.put("description", encodedProjectDescription);

  executeGetRequest(prepareGetRequest(azkabanServerUrl + "/manager", sessionId, params));
}
 
Example #6
Source File: GetClient.java    From javabase with Apache License 2.0 6 votes vote down vote up
public void get2() throws IOException {
    String APIKEY = "4b441cb500f431adc6cc0cb650b4a5d0";
    String INFO = new URLCodec().encode("who are you", "utf-8");
    String requesturl = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
    // 声明httpclient
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet(requesturl);
        response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity, "utf-8");
        log.info(content);
        EntityUtils.consume(entity);
    } catch (Exception e) {
        log.error("" + e);
    } finally {
        if (response != null)
            response.close();
    }
}
 
Example #7
Source File: OldHttpClientApi.java    From javabase with Apache License 2.0 6 votes vote down vote up
public void  get() throws UnsupportedEncodingException {
    HttpClient client = new HttpClient();
    String APIKEY = "4b441cb500f431adc6cc0cb650b4a5d0";
    String INFO =new URLCodec().encode("who are you","utf-8");
    String requesturl = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
    GetMethod getMethod = new GetMethod(requesturl);
    try {
        int stat = client.executeMethod(getMethod);
        if (stat != HttpStatus.SC_OK)
           log.error("get失败!");
        byte[] responseBody = getMethod.getResponseBody();
        String content=new String(responseBody ,"utf-8");
        log.info(content);
    }catch (Exception e){
       log.error(""+e.getLocalizedMessage());
    }finally {
        getMethod.releaseConnection();
    }
}
 
Example #8
Source File: EncodingUtil.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Form-urlencoding routine.
 *
 * The default encoding for all forms is `application/x-www-form-urlencoded'. 
 * A form data set is represented in this media type as follows:
 *
 * The form field names and values are escaped: space characters are replaced 
 * by `+', and then reserved characters are escaped as per [URL]; that is, 
 * non-alphanumeric characters are replaced by `%HH', a percent sign and two 
 * hexadecimal digits representing the ASCII code of the character. Line breaks, 
 * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'.
 * 
 * @param pairs the values to be encoded
 * @param charset the character set of pairs to be encoded
 * 
 * @return the urlencoded pairs
 * @throws UnsupportedEncodingException if charset is not supported
 * 
 * @since 2.0 final
 */
 private static String doFormUrlEncode(NameValuePair[] pairs, String charset)
    throws UnsupportedEncodingException 
 {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < pairs.length; i++) {
        URLCodec codec = new URLCodec();
        NameValuePair pair = pairs[i];
        if (pair.getName() != null) {
            if (i > 0) {
                buf.append("&");
            }
            buf.append(codec.encode(pair.getName(), charset));
            buf.append("=");
            if (pair.getValue() != null) {
                buf.append(codec.encode(pair.getValue(), charset));
            }
        }
    }
    return buf.toString();
}
 
Example #9
Source File: DynamicSolrStoreMappingWrapperFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public String getShards()
{
    try
    {
        URLCodec encoder = new URLCodec();
        StringBuilder builder = new StringBuilder();

        for(ShardInstance instance : slice)
        {
            if (builder.length() > 0)
            {
                builder.append(',');
            }
            Pair<String, Integer> key = new Pair<String, Integer>(instance.getHostName(), instance.getPort());
            HttpClient client = clients.get(key);
            builder.append(encoder.encode(client.getHostConfiguration().getProtocol().getScheme() +  "://", "UTF-8"));
            builder.append(encoder.encode(instance.getHostName(), "UTF-8"));
            builder.append(':');
            builder.append(encoder.encode("" + instance.getPort(), "UTF-8"));
            if(!instance.getBaseUrl().startsWith("/"))
            {
                builder.append('/');
            }
            builder.append(encoder.encode(instance.getBaseUrl(), "UTF-8"));
        }
        
        return builder.toString();
    }
    catch (UnsupportedEncodingException e)
    {
        throw new LuceneQueryParserException("", e);
    }
}
 
Example #10
Source File: EscapeUrl.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void escape_url_with_apache_commons () throws EncoderException {

	URLCodec codec = new URLCodec();
	String urlEscaped = codec.encode(URL_TO_ESCAPE);
	
	assertEquals("http%3A%2F%2Fwww.leveluplunch.com%3Fsomevar%3Dabc123%26someothervar", urlEscaped);
}
 
Example #11
Source File: SolrStoreMappingWrapperTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testSingleShard() throws UnsupportedEncodingException
{
    URLCodec encoder = new URLCodec();
    String shards = unshardedWrapper.getShards();
    assertNotNull(shards);
    assertEquals(encoder.encode("https://", "UTF-8")
            + "common:999" + encoder.encode("/solr4", "UTF-8"), shards);
}
 
Example #12
Source File: SOLRAPIClient.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public AlfrescoModel getModel(String coreName, QName modelName) throws AuthenticationException, IOException
{
    // If the model is new to the SOLR side the prefix will be unknown so we can not generate prefixes for the request!
    // Always use the full QName with explicit URI
    StringBuilder url = new StringBuilder(GET_MODEL);

    URLCodec encoder = new URLCodec();
    // must send the long name as we may not have the prefix registered
    url.append("?modelQName=").append(encoder.encode(modelName.toString(), "UTF-8"));
    
    GetRequest req = new GetRequest(url.toString());

    Response response = null;
    try
    {
        response = repositoryHttpClient.sendRequest(req);
        if(response.getStatus() != HttpStatus.SC_OK)
        {
            throw new AlfrescoRuntimeException(coreName + " GetModel return status is " + response.getStatus());
        }

        return new AlfrescoModel(M2Model.createModel(response.getContentAsStream()),
                Long.valueOf(response.getHeader(CHECKSUM_HEADER)));
    }
    finally
    {
        if(response != null)
        {
            response.release();
        }
    }
}
 
Example #13
Source File: Urls.java    From knox with Apache License 2.0 5 votes vote down vote up
public static String encode( String str ) {
  URLCodec codec = new URLCodec();
  try {
    return codec.encode( str, StandardCharsets.UTF_8.name() );
  } catch( UnsupportedEncodingException e ) {
    throw new IllegalArgumentException( e );
  }
}
 
Example #14
Source File: Urls.java    From knox with Apache License 2.0 5 votes vote down vote up
public static String decode( String str ) {
  URLCodec codec = new URLCodec();
  try {
    return codec.decode( str, StandardCharsets.UTF_8.name() );
  } catch( UnsupportedEncodingException | DecoderException e ) {
    throw new IllegalArgumentException( e );
  }
}
 
Example #15
Source File: LinkResolver.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the protocol, if it exists, from the front and
 * removes any random encoding characters
 * Extend this to do other url cleaning/pre-processing
 *
 * @param url - The String URL to normalize
 * @return normalizedUrl - The String URL that has no junk or surprises
 */
public static String normalizeURL(String url) {
    // Decode URL to remove any %20 type stuff
    String normalizedUrl = url;
    try {

        // Replaced URLDecode with commons-codec b/c of failing tests

        URLCodec codec = new URLCodec();

        normalizedUrl = codec.decode(url);

        // Remove the protocol, http:// ftp:// or similar from the front
        if (normalizedUrl.contains("://"))
            normalizedUrl = normalizedUrl.split(":/{2}")[1];

    } catch (NullPointerException npe) {
        System.err.println("NPE Decoding URL. Decoding skipped.");
        npe.printStackTrace();
    } catch (Throwable e) {
        System.err.println("Misc error Decoding URL. Decoding skipped.");
        e.printStackTrace();
    }


    // Room here to do more pre-processing

    return normalizedUrl;
}
 
Example #16
Source File: LuceneIndexHandler.java    From FXDesktopSearch with Apache License 2.0 5 votes vote down vote up
private String encode(final String aValue) {
    final var theURLCodec = new URLCodec();
    try {
        return theURLCodec.encode(aValue);
    } catch (final EncoderException e) {
        return null;
    }
}
 
Example #17
Source File: KRADUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Generate the request parameter portion of the url based on the map of key value pairs passed in
 *
 * @param requestParameters the request parameters to use in the string
 * @return a request parameter string starting with "?" with "&" separators, or blank if the mapped passed in is
 * blank
 */
public static String getRequestStringFromMap(Map<String, String> requestParameters) {
    String requestString = "";

    if (requestParameters.isEmpty()) {
        return requestString;
    }

    URLCodec urlCodec = new URLCodec(KRADConstants.DEFAULT_ENCODING);

    for (String key : requestParameters.keySet()) {
        String value = null;
        try {
            value = urlCodec.encode(requestParameters.get(key));
        } catch (EncoderException e) {
            throw new RuntimeException("Unable to encode parameter name or value: " + key + "=" + value, e);
        }

        if (StringUtils.isNotBlank(requestString)) {
            requestString = requestString + "&";
        }

        requestString = requestString + key + "=" + value;
    }

    return "?" + requestString;
}
 
Example #18
Source File: URIUtil.java    From bintray-client-java with Apache License 2.0 5 votes vote down vote up
/**
 * Unescape and decode a given string regarded as an escaped string with the
 * default protocol charset.
 *
 * @param escaped a string
 * @return the unescaped string
 * @throws HttpException if the string cannot be decoded (invalid)
 */
public static String decode(String escaped) throws HttpException {
    try {
        byte[] rawdata = URLCodec.decodeUrl(EncodingUtils.getAsciiBytes(escaped));
        return EncodingUtils.getString(rawdata, UTF8_CHARSET_NAME);
    } catch (DecoderException e) {
        throw new HttpException(e.getMessage());
    }
}
 
Example #19
Source File: UrlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Unescapes and decodes the specified string.
 *
 * @param escaped the string to be unescaped and decoded
 * @return the unescaped and decoded string
 */
public static String decode(final String escaped) {
    try {
        final byte[] bytes = escaped.getBytes(US_ASCII);
        final byte[] bytes2 = URLCodec.decodeUrl(bytes);
        return new String(bytes2, UTF_8);
    }
    catch (final DecoderException e) {
        // Should never happen.
        throw new RuntimeException(e);
    }
}
 
Example #20
Source File: UrlUtils.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Unescapes and decodes the specified string.
 *
 * @param escaped the string to be unescaped and decoded
 * @return the unescaped and decoded string
 */
public static String decode(final String escaped) {
    try {
        final byte[] bytes = escaped.getBytes(US_ASCII);
        final byte[] bytes2 = URLCodec.decodeUrl(bytes);
        return new String(bytes2, UTF_8);
    }
    catch (final DecoderException e) {
        // Should never happen.
        throw new RuntimeException(e);
    }
}
 
Example #21
Source File: URIUtil.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decode url encoded string.
 * @param escaped encoded string
 * @return decoded string
 * @throws IOException on error
 */
public static String decode(String escaped) throws IOException {
    try {
        return getString(URLCodec.decodeUrl(getAsciiBytes(escaped)));
    } catch (DecoderException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example #22
Source File: TestURIBuilder.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void testDecodePlus() throws IOException, URISyntaxException, DecoderException {
    URI uri = new URI("https://host/encoded+plus");
    System.out.println(uri.getPath());
    System.out.println(URIUtil.decode(uri.getPath()));

    String decoded = new String(URLCodec.decodeUrl(uri.getPath().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    assertEquals(decoded, URIUtil.decode(uri.getPath()));
}
 
Example #23
Source File: TestURIBuilder.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void testDecodeSpecial() throws IOException, URISyntaxException, DecoderException {
    URI uri = new URI("https://host/@");
    System.out.println(uri.getPath());
    System.out.println(URIUtil.decode(uri.getPath()));

    String decoded = new String(URLCodec.decodeUrl(uri.getPath().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    assertEquals(decoded, URIUtil.decode(uri.getPath()));
}
 
Example #24
Source File: SolrQueryHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected String buildStatsUrl(StatsParameters searchParameters, String baseUrl, Locale locale, SolrStoreMappingWrapper mapping) throws UnsupportedEncodingException
{
    URLCodec encoder = new URLCodec();
    StringBuilder url = new StringBuilder();
    String languageUrlFragment = SolrClientUtil.extractLanguageFragment(languageMappings, searchParameters.getLanguage());
    
    url.append(baseUrl);
    url.append("/").append(languageUrlFragment);
    url.append("?wt=").append(encoder.encode("json", "UTF-8"));
    url.append("&locale=").append(encoder.encode(locale.toString(), "UTF-8"));
    
    url.append(buildSortParameters(searchParameters, encoder));
    
    url.append("&stats=true");
    url.append("&rows=0");
    if (!StringUtils.isBlank(searchParameters.getFilterQuery()))
    {
        url.append("?fq=").append(encoder.encode(searchParameters.getFilterQuery(), "UTF-8")); 
    }

    for(Entry<String, String> entry : searchParameters.getStatsParameters().entrySet())
    {
        url.append("&stats.").append(entry.getKey()).append("=").append(encoder.encode(entry.getValue(), "UTF-8"));
    }
    
    if((mapping != null) && ((searchParameters.getStores().size() > 1) || (mapping.isSharded())))
    {
        url.append("&shards=");
        buildShards(url, searchParameters.getStores());
    }
    
    return url.toString();
}
 
Example #25
Source File: TestURIBuilder.java    From davmail with GNU General Public License v2.0 4 votes vote down vote up
public void testEncodeSpecial() {
    BitSet ical_allowed_abs_path = new BitSet(256);

    ical_allowed_abs_path.or(org.apache.commons.httpclient.URI.allowed_abs_path);
    ical_allowed_abs_path.clear('@');

    String path = "[email protected]";
    String encoded = URIUtil.encode(path, ical_allowed_abs_path);

    System.out.println(encoded);

    String newEncoded = new String(URLCodec.encodeUrl(ical_allowed_abs_path, path.getBytes(Consts.UTF_8)), Consts.UTF_8);
    System.out.println(newEncoded);

    assertEquals(newEncoded, encoded);
}
 
Example #26
Source File: HttpUriDissector.java    From logparser with Apache License 2.0 4 votes vote down vote up
@Override
public void dissect(final Parsable<?> parsable, final String inputname) throws DissectionFailure {
    final ParsedField field = parsable.getParsableField(INPUT_TYPE, inputname);

    String uriString = field.getValue().getString();
    if (uriString == null || uriString.isEmpty()) {
        return; // Nothing to do here
    }

    // First we cleanup the URI so we fail less often over 'garbage' URIs.
    // See: https://stackoverflow.com/questions/11038967/brackets-in-a-request-url-are-legal-but-not-in-a-uri-java
    uriString = new String(URLCodec.encodeUrl(BAD_URI_CHARS, uriString.getBytes(UTF_8)), US_ASCII);

    // Before we hand it to the standard parser we hack it around a bit so we can parse
    // nasty edge cases that are illegal yet do occur in real clickstreams.
    // Also we force the query string to start with ?& so the returned query string starts with &
    // Which leads to more consistent output after parsing.
    int firstQuestionMark = uriString.indexOf('?');
    int firstAmpersand = uriString.indexOf('&');
    // Now we can have one of 3 situations:
    // 1) No query string
    // 2) Query string starts with a '?'
    //      (and optionally followed by one or more '&' or '?' )
    // 3) Query string starts with a '&'. This is invalid but does occur!
    // We may have ?x=x&y=y?z=z so we normalize it always
    // to:  ?&x=x&y=y&z=z
    if (firstAmpersand != -1 || firstQuestionMark != -1) {
        uriString = uriString.replaceAll("\\?", "&");
        uriString = uriString.replaceFirst("&", "?&");
    }

    // We find that people muck up the URL by putting % signs in the URLs that are NOT escape sequences
    // So any % that is not followed by a two 'hex' letters is fixed
    uriString = BAD_EXCAPE_PATTERN.matcher(uriString).replaceAll("%25$1");
    uriString = BAD_EXCAPE_PATTERN.matcher(uriString).replaceAll("%25$1");

    // We have URIs with fragments like this:
    //    /path/?_requestid=1234#x3D;12341234&Referrer&#x3D;blablabla
    // So first we repair the broken encoded char
    uriString = ALMOST_HTML_ENCODED.matcher(uriString).replaceAll("$1&$2");
    uriString = StringEscapeUtils.unescapeHtml4(uriString);
    // And we see URIs with this:
    //    /path/?Referrer=ADV1234#&f=API&subid=#&name=12341234
    uriString = EQUALS_HASH_PATTERN.matcher(uriString).replaceAll("=");
    uriString = HASH_AMP_PATTERN.matcher(uriString).replaceAll("&");

    // If we still have multiple '#' in here we replace them with something else: '~'
    while (true) {
        Matcher doubleHashMatcher = DOUBLE_HASH_PATTERN.matcher(uriString);
        if (!doubleHashMatcher.find()) {
            break;
        }
        uriString = doubleHashMatcher.replaceAll("~$1#");
    }

    boolean isUrl = true;
    URI uri;
    try {
        if (uriString.charAt(0) == '/') {
            uri = URI.create("dummy-protocol://dummy.host.name" + uriString);
            isUrl = false; // I.e. we do not return the values we just faked.
        } else {
            uri = URI.create(uriString);
        }
    } catch (IllegalArgumentException e) {
        throw new DissectionFailure("Failed to parse URI >>" + field.getValue().getString()+"<< because of : " +e.getMessage());
    }

    if (wantQuery || wantPath || wantRef) {
        if (wantQuery) {
            String query = uri.getRawQuery();
            if (query == null) {
                query = "";
            }
            parsable.addDissection(inputname, "HTTP.QUERYSTRING", "query", query);
        }
        if (wantPath) {
            parsable.addDissection(inputname, "HTTP.PATH", "path", uri.getPath());
        }
        if (wantRef) {
            parsable.addDissection(inputname, "HTTP.REF", "ref", uri.getFragment());
        }
    }

    if (isUrl) {
        if (wantProtocol) {
            parsable.addDissection(inputname, "HTTP.PROTOCOL", "protocol", uri.getScheme());
        }
        if (wantUserinfo) {
            parsable.addDissection(inputname, "HTTP.USERINFO", "userinfo", uri.getUserInfo());
        }
        if (wantHost) {
            parsable.addDissection(inputname, "HTTP.HOST", "host", uri.getHost());
        }
        if (wantPort) {
            if (uri.getPort() != -1) {
                parsable.addDissection(inputname, "HTTP.PORT", "port", uri.getPort());
            }
        }
    }
}
 
Example #27
Source File: SolrQueryHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private StringBuffer buildSortParameters(BasicSearchParameters searchParameters, URLCodec encoder)
            throws UnsupportedEncodingException
{
    StringBuffer sortBuffer = new StringBuffer();
    for (SortDefinition sortDefinition : searchParameters.getSortDefinitions())
    {
        if (sortBuffer.length() == 0)
        {
            sortBuffer.append("&sort=");
        }
        else
        {
            sortBuffer.append(encoder.encode(", ", "UTF-8"));
        }
        // MNT-8557 fix, manually replace ' ' with '%20'
        // The sort can be different, see MNT-13742
        switch (sortDefinition.getSortType())
        {
            case DOCUMENT:
                sortBuffer.append(encoder.encode("_docid_", "UTF-8")).append(encoder.encode(" ", "UTF-8"));
                break;
            case SCORE:
                sortBuffer.append(encoder.encode("score", "UTF-8")).append(encoder.encode(" ", "UTF-8"));
                break;
            case FIELD:
            default:
                sortBuffer.append(encoder.encode(sortDefinition.getField().replaceAll(" ", "%20"), "UTF-8")).append(encoder.encode(" ", "UTF-8"));
                break;
        }
     if (sortDefinition.isAscending())
        {
            sortBuffer.append(encoder.encode("asc", "UTF-8"));
        }
        else
        {
            sortBuffer.append(encoder.encode("desc", "UTF-8"));
        }

    }
    return sortBuffer;
}
 
Example #28
Source File: SolrQueryHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void buildPivotParameters(SearchParameters searchParameters, URLCodec encoder, StringBuilder url) throws UnsupportedEncodingException
{
    if (searchParameters.getPivots() != null && !searchParameters.getPivots().isEmpty())
    {
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        for (List<String> pivotKeys:searchParameters.getPivots())
        {
            List<String> pivotsList = new ArrayList<>();
            pivotsList.addAll(pivotKeys);
            url.append("&facet.pivot=");

            StringBuilder prefix = new StringBuilder("{! ");

            if (searchParameters.getStats() != null && !searchParameters.getStats().isEmpty())
            {
                for (StatsRequestParameters aStat:searchParameters.getStats())
                {
                    if (pivotKeys.contains(aStat.getLabel()))
                    {
                        prefix.append("stats="+aStat.getLabel()+" ");
                        pivotsList.remove(aStat.getLabel());
                        break; //only do it once
                    }
                }
            }

            if (searchParameters.getRanges() != null && !searchParameters.getRanges().isEmpty())
            {
                for (RangeParameters aRange:searchParameters.getRanges())
                {
                    Optional<String> found = pivotKeys.stream().filter(aKey -> aKey.equals(aRange.getLabel())).findFirst();

                    if (found.isPresent())
                    {
                        prefix.append("range="+found.get()+" ");
                        pivotsList.remove(found.get());
                        break; //only do it once
                    }
                }
            }

            if (prefix.length() > 3)  //We have add something
            {
                url.append(encoder.encode(prefix.toString().trim(), "UTF-8"));
                url.append(encoder.encode("}", "UTF-8"));
            }
            url.append(encoder.encode(String.join(",", pivotsList), "UTF-8"));
        }
    }
}
 
Example #29
Source File: ExplicitSolrStoreMappingWrapper.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String getShards2()
{
    try
    {
        URLCodec encoder = new URLCodec();
        StringBuilder builder = new StringBuilder();

        for (int shard = 0; shard < wrapped.getNumShards(); shard++)
        {
            int position = random.nextInt(wrapped.getReplicationFactor());
            List<Integer> nodeInstances = policy.getNodeInstancesForShardId(shard);
            Integer nodeId = nodeInstances.get(position);
            
            if (builder.length() > 0)
            {
                builder.append(',');
            }
            HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs.toArray(new HttpClientAndBaseUrl[0])[nodeId-1];
            builder.append(encoder.encode(httpClientAndBaseUrl.getProtocol() +  "://", "UTF-8"));
            builder.append(encoder.encode(httpClientAndBaseUrl.getHost(), "UTF-8"));
            builder.append(':');
            builder.append(encoder.encode("" + httpClientAndBaseUrl.getPort(), "UTF-8"));
            if (httpClientAndBaseUrl.getBaseUrl().startsWith("/"))
            {
                builder.append(encoder.encode(httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            }
            else
            {
                builder.append(encoder.encode("/" + httpClientAndBaseUrl.getBaseUrl(), "UTF-8"));
            }

            if (isSharded()) builder.append('-').append(shard);

        }
        return builder.toString();
    }
    catch (UnsupportedEncodingException e)
    {
        throw new LuceneQueryParserException("", e);
    }
}
 
Example #30
Source File: SolrQueryHTTPClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void buildRangeParameters(SearchParameters searchParameters, URLCodec encoder, StringBuilder url) throws UnsupportedEncodingException
{
    if (searchParameters.getRanges() != null && !searchParameters.getRanges().isEmpty())
    {
        List<RangeParameters> ranges = searchParameters.getRanges();
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        
        for(RangeParameters facetRange : ranges)
        {
            String fieldName = facetRange.getField();
            boolean isDate = false;
            PropertyDefinition propertyDef = QueryParserUtils.matchPropertyDefinition(searchParameters.getNamespace(),
                    namespaceDAO, dictionaryService, fieldName);
            if (propertyDef != null && (propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)
                    || propertyDef.getDataType().getName().equals(DataTypeDefinition.DATE)))
            {
                isDate = true;
            }
            
            
            IntervalSet rangeSet =
                    parseDateInterval(
                            new IntervalSet(facetRange.getStart(), 
                                            facetRange.getEnd(), 
                                            facetRange.getGap(), 
                                            null, 
                                            null), isDate);
            url.append("&facet.range=");

            if(facetRange.getLabel()!= null && !facetRange.getLabel().isEmpty())
            {
                url.append(encoder.encode("{!", "UTF-8"));
                url.append(encoder.encode(String.format("tag=%s ",facetRange.getLabel()), "UTF-8"));
                url.append(encoder.encode("}", "UTF-8"));
            }

            url.append(encoder.encode(facetRange.getField(), "UTF-8"));

            //Check if date and if inclusive or not
            url.append(String.format("&f.%s.facet.range.start=",fieldName)).append(encoder.encode(""+ rangeSet.getStart(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.end=",fieldName)).append(encoder.encode(""+ rangeSet.getEnd(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.gap=",fieldName)).append(encoder.encode(""+ rangeSet.getLabel(), "UTF-8"));
            url.append(String.format("&f.%s.facet.range.hardend=",fieldName)).append(encoder.encode("" + facetRange.isHardend(), "UTF-8"));
            if(facetRange.getInclude() != null && !facetRange.getInclude().isEmpty())
            {
                for(String include : facetRange.getInclude())
                {
                    url.append(String.format("&f.%s.facet.range.include=",fieldName)).append(encoder.encode("" + include, "UTF-8"));
                }
            }
            if(facetRange.getOther() != null && !facetRange.getOther().isEmpty())
            {
                for(String other : facetRange.getOther())
                {
                    url.append(String.format("&f.%s.facet.range.other=",fieldName)).append(encoder.encode("" + other, "UTF-8"));
                }
            }
            if(!facetRange.getExcludeFilters().isEmpty())
            {
                url.append("&facet.range=");
                if (facetRange.getExcludeFilters() != null && !facetRange.getExcludeFilters().isEmpty())
                {
                    StringBuilder prefix = new StringBuilder("{!ex=");
                    Iterator<String> itr = facetRange.getExcludeFilters().iterator();
                    while(itr.hasNext())
                    {
                        String val = itr.next();
                        prefix.append(val);
                        if(itr.hasNext())
                        {
                            prefix.append(",");
                        }
                    }
                    prefix.append("}");
                    url.append(prefix);
                    url.append(fieldName);
                }
                
            }
        }
    }
}