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

The following examples show how to use org.apache.commons.codec.net.URLCodec. These examples are extracted from open source projects. 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 Project: htmlunit   Source File: DataUrlDecoder.java    License: 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 Project: HtmlUnit-Android   Source File: DataUrlDecoder.java    License: 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 3
Source Project: tus-java-server   Source File: UploadId.java    License: 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 4
Source Project: javabase   Source File: GetClient.java    License: 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 5
Source Project: javabase   Source File: OldHttpClientApi.java    License: 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 6
/**
 * 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 7
Source Project: incubator-gobblin   Source File: AzkabanAjaxAPIClient.java    License: 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 8
Source Project: SeaCloudsPlatform   Source File: HttpHelper.java    License: 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 9
Source Project: htmlunit   Source File: UrlUtils.java    License: 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 10
Source Project: HtmlUnit-Android   Source File: UrlUtils.java    License: 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 11
Source Project: davmail   Source File: URIUtil.java    License: 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 12
Source Project: davmail   Source File: TestURIBuilder.java    License: 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 13
Source Project: davmail   Source File: TestURIBuilder.java    License: 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 14
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 15
@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 16
@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 17
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 18
Source Project: knox   Source File: Urls.java    License: 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 19
Source Project: knox   Source File: Urls.java    License: 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 20
Source Project: streams   Source File: LinkResolver.java    License: 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 21
Source Project: FXDesktopSearch   Source File: LuceneIndexHandler.java    License: 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 22
Source Project: rice   Source File: KRADUtils.java    License: 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 23
Source Project: bintray-client-java   Source File: URIUtil.java    License: 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 24
Source Project: levelup-java-examples   Source File: EscapeUrl.java    License: 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 25
Source Project: davmail   Source File: TestURIBuilder.java    License: 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
/**
 * Builds most of the Url parameters for a Solr Http request.
 * @param searchParameters
 * @param isSharded
 * @param encoder
 * @param url
 * @throws UnsupportedEncodingException
 */
public void buildUrlParameters(SearchParameters searchParameters, boolean isSharded, URLCodec encoder, StringBuilder url)
            throws UnsupportedEncodingException
{
    Locale locale = SolrClientUtil.extractLocale(searchParameters);
    url.append("&df=").append(encoder.encode(searchParameters.getDefaultFieldName(), "UTF-8"));
    url.append("&start=").append(encoder.encode("" + searchParameters.getSkipCount(), "UTF-8"));

    url.append("&locale=");
    url.append(encoder.encode(locale.toString(), "UTF-8"));
    url.append("&").append(SearchParameters.ALTERNATIVE_DICTIONARY).append("=").append(alternativeDictionary);
    for(String paramName : searchParameters.getExtraParameters().keySet())
    {
        url.append("&").append(paramName).append("=").append(searchParameters.getExtraParameters().get(paramName));
    }
    StringBuffer sortBuffer = buildSortParameters(searchParameters, encoder);
    url.append(sortBuffer);

    if(searchParameters.getPermissionEvaluation() != PermissionEvaluationMode.NONE)
    {
        url.append("&fq=").append(encoder.encode("{!afts}AUTHORITY_FILTER_FROM_JSON", "UTF-8"));
    }

    if(searchParameters.getExcludeTenantFilter() == false)
    {
        url.append("&fq=").append(encoder.encode("{!afts}TENANT_FILTER_FROM_JSON", "UTF-8"));
    }

    if(searchParameters.getTimezone() != null && !searchParameters.getTimezone().isEmpty())
    {
        url.append("&TZ=").append(encoder.encode(searchParameters.getTimezone(), "UTF-8"));
    }

    // filter queries
    for(String filterQuery : searchParameters.getFilterQueries())
    {
        if (!filterQuery.startsWith("{!afts"))
        {
            filterQuery = "{!afts}"+filterQuery;
        }
        url.append("&fq=").append(encoder.encode(filterQuery, "UTF-8"));
    }

    buildFacetParameters(searchParameters, isSharded, encoder, url);
    buildPivotParameters(searchParameters, encoder, url);
    buildStatsParameters(searchParameters, encoder, url);
    buildFacetIntervalParameters(searchParameters, encoder, url);
    buildRangeParameters(searchParameters, encoder, url);
    buildHightlightParameters(searchParameters, encoder, url);
}
 
Example 27
protected void buildFacetParameters(SearchParameters searchParameters, boolean isSharded, URLCodec encoder, StringBuilder url)
            throws UnsupportedEncodingException
{
    if(searchParameters.getFieldFacets().size() > 0 || searchParameters.getFacetQueries().size() > 0)
    {
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        for(FieldFacet facet : searchParameters.getFieldFacets())
        {
            url.append("&facet.field=");
            String field = facet.getField();
            StringBuilder prefix = new StringBuilder("{!afts ");

            int startIndex = field.startsWith("{!afts")?7:0;

            if (facet.getExcludeFilters() != null && !facet.getExcludeFilters().isEmpty())
            {
                prefix.append("ex="+String.join(",", facet.getExcludeFilters())+" ");
            }

            if (facet.getLabel() != null && !facet.getLabel().isEmpty())
            {
                prefix.append("key="+facet.getLabel()+" ");
            }

            if (startIndex!=0)
            {
                int endIndex = field.indexOf("}");
                prefix.append(field.substring(startIndex,endIndex>startIndex?endIndex:startIndex));
                field = field.substring(endIndex+1);
            }

            if (prefix.length() > 7)
            {
                url.append(encoder.encode(prefix.toString().trim(), "UTF-8"));
                url.append(encoder.encode("}", "UTF-8"));
            }

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

            if(facet.getEnumMethodCacheMinDF() != 0)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.enum.cache.minDf", "UTF-8")).append("=").append(encoder.encode(""+facet.getEnumMethodCacheMinDF(), "UTF-8"));
            }
            int facetLimit;
            if(facet.getLimitOrNull() == null)
            {
                if(isSharded())
                {
                    facetLimit = defaultShardedFacetLimit;
                }
                else
                {
                    facetLimit = defaultUnshardedFacetLimit;
                }
            }
            else
            {
                facetLimit = facet.getLimitOrNull().intValue();
            }
            url.append("&").append(encoder.encode("f."+field+".facet.limit", "UTF-8")).append("=").append(encoder.encode(""+facetLimit, "UTF-8"));
            if(facet.getMethod() != null)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.method", "UTF-8")).append("=").append(encoder.encode(facet.getMethod()== FieldFacetMethod.ENUM ?  "enum" : "fc", "UTF-8"));
            }
            if(facet.getMinCount() != 0)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.mincount", "UTF-8")).append("=").append(encoder.encode(""+facet.getMinCount(), "UTF-8"));
            }
            if(facet.getOffset() != 0)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.offset", "UTF-8")).append("=").append(encoder.encode(""+facet.getOffset(), "UTF-8"));
            }
            if(facet.getPrefix() != null)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.prefix", "UTF-8")).append("=").append(encoder.encode(""+facet.getPrefix(), "UTF-8"));
            }
            if(facet.getSort() != null)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.sort", "UTF-8")).append("=").append(encoder.encode(facet.getSort() == FieldFacetSort.COUNT ? "count" : "index", "UTF-8"));
            }
            if(facet.isCountDocsMissingFacetField() != false)
            {
                url.append("&").append(encoder.encode("f."+field+".facet.missing", "UTF-8")).append("=").append(encoder.encode(""+facet.isCountDocsMissingFacetField(), "UTF-8"));
            }

        }
        for(String facetQuery : searchParameters.getFacetQueries())
        {
            if (!facetQuery.startsWith("{!afts"))
            {
                facetQuery = "{!afts}"+facetQuery;
            }
            url.append("&facet.query=").append(encoder.encode(facetQuery, "UTF-8"));
        }
    }
}
 
Example 28
protected void buildStatsParameters(SearchParameters searchParameters, URLCodec encoder, StringBuilder url) throws UnsupportedEncodingException
{
    if (searchParameters.getStats() != null && !searchParameters.getStats().isEmpty())
    {
        url.append("&stats=").append(encoder.encode("true", "UTF-8"));

        for (StatsRequestParameters aStat:searchParameters.getStats())
        {
            url.append("&stats.field=");
            StringBuilder prefix = new StringBuilder("{! ");

            if (aStat.getExcludeFilters() != null && !aStat.getExcludeFilters().isEmpty())
            {
                prefix.append("ex="+String.join(",", aStat.getExcludeFilters())+" ");
            }

            if (aStat.getLabel() != null && !aStat.getLabel().isEmpty())
            {
                prefix.append("tag="+aStat.getLabel()+" ");
                prefix.append("key="+aStat.getLabel()+" ");
            }

            if (aStat.getPercentiles() != null && !aStat.getPercentiles().isEmpty())
            {
                StringJoiner joiner = new StringJoiner(",");
                for (Float aFloat: aStat.getPercentiles()) {
                    joiner.add(aFloat.toString());
                }
                prefix.append("percentiles='"+joiner.toString()+"' ");
            }

            if (aStat.getCardinality())
            {
                prefix.append("cardinality="+aStat.getCardinalityAccuracy()+" ");
            }

            prefix.append("countDistinct="+aStat.getCountDistinct()+" ");
            prefix.append("distinctValues="+aStat.getDistinctValues()+" ");
            prefix.append("min="+aStat.getMin()+" ");
            prefix.append("max="+aStat.getMax()+" ");
            prefix.append("sum="+aStat.getSum()+" ");
            prefix.append("count="+aStat.getCountValues()+" ");
            prefix.append("missing="+aStat.getMissing()+" ");
            prefix.append("sumOfSquares="+aStat.getSumOfSquares()+" ");
            prefix.append("mean="+aStat.getMean()+" ");
            prefix.append("stddev="+aStat.getStddev()+" ");

            url.append(encoder.encode(prefix.toString().trim(), "UTF-8"));
            url.append(encoder.encode("}", "UTF-8"));

            url.append(encoder.encode(aStat.getField(), "UTF-8"));
        }
    }
}
 
Example 29
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 30
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);
                }
                
            }
        }
    }
}