Example usage for org.apache.commons.codec.net URLCodec URLCodec

List of usage examples for org.apache.commons.codec.net URLCodec URLCodec

Introduction

In this page you can find the example usage for org.apache.commons.codec.net URLCodec URLCodec.

Prototype

public URLCodec() 

Source Link

Document

Default constructor.

Usage

From source file:com.rackspacecloud.client.cloudfiles.FilesClient.java

/**
 * URI//w ww. jav  a2  s  . co m
 * @param str
 *           URI
 * @return 
 */
public static String unencodeURI(String str) {
    URLCodec codec = new URLCodec();
    try {
        return codec.decode(str);
    } catch (DecoderException ee) {
        logger.warn("Error trying to encode string for URI", ee);
        return str;
    }

}

From source file:org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper.java

private String getShards1() {
    try {/*from w w w  .j ava 2s . com*/
        URLCodec encoder = new URLCodec();
        StringBuilder builder = new StringBuilder();

        Set<Integer> shards = new HashSet<Integer>();
        for (int i = 0; i < httpClientsAndBaseURLs.size(); i += wrapped.getReplicationFactor()) {
            for (Integer shardId : policy.getShardIdsForNode(i + 1)) {
                if (!shards.contains(shardId % wrapped.getNumShards())) {
                    if (shards.size() > 0) {
                        builder.append(',');
                    }
                    HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs
                            .toArray(new HttpClientAndBaseUrl[0])[i];
                    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"));
                    }

                    builder.append('-').append(shardId);

                    shards.add(shardId % wrapped.getNumShards());
                }

            }
        }
        return builder.toString();
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.repo.search.impl.solr.ExplicitSolrStoreMappingWrapper.java

private String getShards2() {
    try {//www.  j  a v  a  2  s. c  o  m
        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.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"));
            }

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

        }
        return builder.toString();
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.repo.search.impl.solr.SolrAdminHTTPClient.java

public JSONObject execute(String relativeHandlerPath, HashMap<String, String> args) {
    ParameterCheck.mandatory("relativeHandlerPath", relativeHandlerPath);
    ParameterCheck.mandatory("args", args);

    String path = getPath(relativeHandlerPath);
    try {/*from   w w  w.  j  a v  a 2s. com*/
        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();

        for (String key : args.keySet()) {
            String value = args.get(key);
            if (url.length() == 0) {
                url.append(path);
                url.append("?");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            } else {
                url.append("&");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            }

        }

        // PostMethod post = new PostMethod(url.toString());
        GetMethod get = new GetMethod(url.toString());

        try {
            httpClient.executeMethod(get);

            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                    || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }

            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException(
                        "Request failed " + get.getStatusCode() + " " + url.toString());
            }

            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            return json;
        } finally {
            get.releaseConnection();
        }
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
        throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
        throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java

protected String buildStatsUrl(StatsParameters searchParameters, String baseUrl, Locale locale,
        SolrStoreMappingWrapper mapping) throws UnsupportedEncodingException {
    URLCodec encoder = new URLCodec();
    StringBuilder url = new StringBuilder();
    String languageUrlFragment = extractLanguageFragment(searchParameters.getLanguage());

    url.append(baseUrl);/*from w w  w  . java2s. c o  m*/
    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()))) {
        boolean requiresSeparator = false;
        url.append("&shards=");
        for (StoreRef storeRef : searchParameters.getStores()) {
            SolrStoreMappingWrapper storeMapping = extractMapping(storeRef);

            if (requiresSeparator) {
                url.append(',');
            } else {
                requiresSeparator = true;
            }

            url.append(storeMapping.getShards());

        }
    }

    return url.toString();
}

From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java

public ResultSet executeQuery(final SearchParameters searchParameters, String language) {
    if (repositoryState.isBootstrapping()) {
        throw new AlfrescoRuntimeException(
                "SOLR queries can not be executed while the repository is bootstrapping");
    }/*from  ww  w .j  av  a2s. c  o m*/

    try {
        StoreRef store = extractStoreRef(searchParameters);
        SolrStoreMappingWrapper mapping = extractMapping(store);
        Locale locale = extractLocale(searchParameters);

        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();

        Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
        HttpClient httpClient = httpClientAndBaseUrl.getFirst();

        url.append(httpClientAndBaseUrl.getSecond());

        String languageUrlFragment = extractLanguageFragment(language);
        if (!url.toString().endsWith("/")) {
            url.append("/");
        }
        url.append(languageUrlFragment);

        // Send the query in JSON only
        // url.append("?q=");
        // url.append(encoder.encode(searchParameters.getQuery(), "UTF-8"));
        url.append("?wt=").append(encoder.encode("json", "UTF-8"));
        url.append("&fl=").append(encoder.encode("DBID,score", "UTF-8"));

        if ((searchParameters.getStores().size() > 1) || (mapping.isSharded())) {
            boolean requiresSeparator = false;
            url.append("&shards=");
            for (StoreRef storeRef : searchParameters.getStores()) {
                SolrStoreMappingWrapper storeMapping = extractMapping(storeRef);

                if (requiresSeparator) {
                    url.append(',');
                } else {
                    requiresSeparator = true;
                }

                url.append(storeMapping.getShards());

            }
        }

        // Emulate old limiting behaviour and metadata
        final LimitBy limitBy;
        int maxResults = -1;
        if (searchParameters.getMaxItems() >= 0) {
            maxResults = searchParameters.getMaxItems();
            limitBy = LimitBy.FINAL_SIZE;
        } else if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE && searchParameters.getLimit() >= 0) {
            maxResults = searchParameters.getLimit();
            limitBy = LimitBy.FINAL_SIZE;
        } else {
            maxResults = searchParameters.getMaxPermissionChecks();
            if (maxResults < 0) {
                maxResults = maximumResultsFromUnlimitedQuery;
            }
            limitBy = LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS;
        }
        url.append("&rows=").append(String.valueOf(maxResults));

        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.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=").append(encoder.encode(facet.getField(), "UTF-8"));
                if (facet.getEnumMethodCacheMinDF() != 0) {
                    url.append("&")
                            .append(encoder.encode("f." + facet.getField() + ".facet.enum.cache.minDf",
                                    "UTF-8"))
                            .append("=").append(encoder.encode("" + facet.getEnumMethodCacheMinDF(), "UTF-8"));
                }
                int facetLimit;
                if (facet.getLimitOrNull() == null) {
                    if (mapping.isSharded()) {
                        facetLimit = defaultShardedFacetLimit;
                    } else {
                        facetLimit = defaultUnshardedFacetLimit;
                    }
                } else {
                    facetLimit = facet.getLimitOrNull().intValue();
                }
                url.append("&").append(encoder.encode("f." + facet.getField() + ".facet.limit", "UTF-8"))
                        .append("=").append(encoder.encode("" + facetLimit, "UTF-8"));
                if (facet.getMethod() != null) {
                    url.append("&").append(encoder.encode("f." + facet.getField() + ".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." + facet.getField() + ".facet.mincount", "UTF-8"))
                            .append("=").append(encoder.encode("" + facet.getMinCount(), "UTF-8"));
                }
                if (facet.getOffset() != 0) {
                    url.append("&").append(encoder.encode("f." + facet.getField() + ".facet.offset", "UTF-8"))
                            .append("=").append(encoder.encode("" + facet.getOffset(), "UTF-8"));
                }
                if (facet.getPrefix() != null) {
                    url.append("&").append(encoder.encode("f." + facet.getField() + ".facet.prefix", "UTF-8"))
                            .append("=").append(encoder.encode("" + facet.getPrefix(), "UTF-8"));
                }
                if (facet.getSort() != null) {
                    url.append("&").append(encoder.encode("f." + facet.getField() + ".facet.sort", "UTF-8"))
                            .append("=").append(encoder.encode(
                                    facet.getSort() == FieldFacetSort.COUNT ? "count" : "index", "UTF-8"));
                }

            }
            for (String facetQuery : searchParameters.getFacetQueries()) {
                if (!facetQuery.startsWith("{!afts")) {
                    facetQuery = "{!afts}" + facetQuery;
                }
                url.append("&facet.query=").append(encoder.encode(facetQuery, "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"));
        }

        // end of field facets

        final String searchTerm = searchParameters.getSearchTerm();
        String spellCheckQueryStr = null;
        if (searchTerm != null && searchParameters.isSpellCheck()) {
            StringBuilder builder = new StringBuilder();
            builder.append("&spellcheck.q=").append(encoder.encode(searchTerm, "UTF-8"));
            builder.append("&spellcheck=").append(encoder.encode("true", "UTF-8"));
            spellCheckQueryStr = builder.toString();
            url.append(spellCheckQueryStr);
        }

        JSONObject body = new JSONObject();
        body.put("query", searchParameters.getQuery());

        // Authorities go over as is - and tenant mangling and query building takes place on the SOLR side

        Set<String> allAuthorisations = permissionService.getAuthorisations();
        boolean includeGroups = includeGroupsForRoleAdmin ? true
                : !allAuthorisations.contains(PermissionService.ADMINISTRATOR_AUTHORITY);

        JSONArray authorities = new JSONArray();
        for (String authority : allAuthorisations) {
            if (includeGroups) {
                authorities.put(authority);
            } else {
                if (AuthorityType.getAuthorityType(authority) != AuthorityType.GROUP) {
                    authorities.put(authority);
                }
            }
        }
        body.put("authorities", authorities);
        body.put("anyDenyDenies", anyDenyDenies);

        JSONArray tenants = new JSONArray();
        tenants.put(tenantService.getCurrentUserDomain());
        body.put("tenants", tenants);

        JSONArray locales = new JSONArray();
        for (Locale currentLocale : searchParameters.getLocales()) {
            locales.put(DefaultTypeConverter.INSTANCE.convert(String.class, currentLocale));
        }
        if (locales.length() == 0) {
            locales.put(I18NUtil.getLocale());
        }
        body.put("locales", locales);

        JSONArray templates = new JSONArray();
        for (String templateName : searchParameters.getQueryTemplates().keySet()) {
            JSONObject template = new JSONObject();
            template.put("name", templateName);
            template.put("template", searchParameters.getQueryTemplates().get(templateName));
            templates.put(template);
        }
        body.put("templates", templates);

        JSONArray allAttributes = new JSONArray();
        for (String attribute : searchParameters.getAllAttributes()) {
            allAttributes.put(attribute);
        }
        body.put("allAttributes", allAttributes);

        body.put("defaultFTSOperator", searchParameters.getDefaultFTSOperator());
        body.put("defaultFTSFieldOperator", searchParameters.getDefaultFTSFieldOperator());
        body.put("queryConsistency", searchParameters.getQueryConsistency());
        if (searchParameters.getMlAnalaysisMode() != null) {
            body.put("mlAnalaysisMode", searchParameters.getMlAnalaysisMode().toString());
        }
        body.put("defaultNamespace", searchParameters.getNamespace());

        JSONArray textAttributes = new JSONArray();
        for (String attribute : searchParameters.getTextAttributes()) {
            textAttributes.put(attribute);
        }
        body.put("textAttributes", textAttributes);

        final int maximumResults = maxResults; //just needed for the final parameter

        return (ResultSet) postSolrQuery(httpClient, url.toString(), body,
                new SolrJsonProcessor<SolrJSONResultSet>() {

                    @Override
                    public SolrJSONResultSet getResult(JSONObject json) {
                        return new SolrJSONResultSet(json, searchParameters, nodeService, nodeDAO, limitBy,
                                maximumResults);
                    }

                }, spellCheckQueryStr);
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
        throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
        throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java

/**
 * @param storeRef/*from   w  ww. j  a  v  a 2  s  .co  m*/
 * @param handler
 * @param params
 * @return
 */
public JSONObject execute(StoreRef storeRef, String handler, HashMap<String, String> params) {
    try {
        SolrStoreMappingWrapper mapping = extractMapping(storeRef);

        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();

        Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
        HttpClient httpClient = httpClientAndBaseUrl.getFirst();

        for (String key : params.keySet()) {
            String value = params.get(key);
            if (url.length() == 0) {
                url.append(httpClientAndBaseUrl.getSecond());

                if (!handler.startsWith("/")) {
                    url.append("/");
                }
                url.append(handler);
                url.append("?");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            } else {
                url.append("&");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            }

        }

        if (mapping.isSharded()) {
            url.append("&shards=");
            url.append(mapping.getShards());
        }

        // PostMethod post = new PostMethod(url.toString());
        GetMethod get = new GetMethod(url.toString());

        try {
            httpClient.executeMethod(get);

            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                    || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }

            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException(
                        "Request failed " + get.getStatusCode() + " " + url.toString());
            }

            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            return json;
        } finally {
            get.releaseConnection();
        }
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
        throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
        throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.repo.solr.EmbeddedSolrTest.java

public JSONObject execute(HashMap<String, String> args) {
    try {/*from   ww w.  j a  va 2 s. c o  m*/
        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();

        for (String key : args.keySet()) {
            String value = args.get(key);
            if (url.length() == 0) {
                url.append(baseUrl);
                url.append("?");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            } else {
                url.append("&");
                url.append(encoder.encode(key, "UTF-8"));
                url.append("=");
                url.append(encoder.encode(value, "UTF-8"));
            }

        }

        GetMethod get = new GetMethod(url.toString());

        try {
            httpClient.executeMethod(get);

            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                    || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }

            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException(
                        "Request failed " + get.getStatusCode() + " " + url.toString());
            }

            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            return json;
        } finally {
            get.releaseConnection();
        }
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
        throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
        throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
        throw new LuceneQueryParserException("", e);
    }
}

From source file:org.alfresco.services.ModelGetterImpl.java

public AlfrescoModel getModel(QName modelName) throws AuthenticationException, IOException, JSONException {
    // 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;/*w w  w . ja  va  2  s  . c  om*/
    try {
        response = repoClient.sendRequest(req);
        if (response.getStatus() != HttpStatus.SC_OK) {
            throw new AlfrescoRuntimeException("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();
        }
    }
}

From source file:org.alfresco.solr.client.SOLRAPIClient.java

public Transactions getTransactions(Long fromCommitTime, Long minTxnId, Long toCommitTime, Long maxTxnId,
        int maxResults, ShardState shardState)
        throws AuthenticationException, IOException, JSONException, EncoderException {
    URLCodec encoder = new URLCodec();

    StringBuilder url = new StringBuilder(GET_TRANSACTIONS_URL);
    StringBuilder args = new StringBuilder();
    if (fromCommitTime != null) {
        args.append("?").append("fromCommitTime").append("=").append(fromCommitTime);
    }//from  www .  j  a v a 2  s. co  m
    if (minTxnId != null) {
        args.append(args.length() == 0 ? "?" : "&").append("minTxnId").append("=").append(minTxnId);
    }
    if (toCommitTime != null) {
        args.append(args.length() == 0 ? "?" : "&").append("toCommitTime").append("=").append(toCommitTime);
    }
    if (maxTxnId != null) {
        args.append(args.length() == 0 ? "?" : "&").append("maxTxnId").append("=").append(maxTxnId);
    }
    if (maxResults != 0 && maxResults != Integer.MAX_VALUE) {
        args.append(args.length() == 0 ? "?" : "&").append("maxResults").append("=").append(maxResults);
    }
    if (shardState != null) {
        args.append(args.length() == 0 ? "?" : "&");
        args.append(encoder.encode("baseUrl")).append("=")
                .append(encoder.encode(shardState.getShardInstance().getBaseUrl()));
        args.append("&").append(encoder.encode("hostName")).append("=")
                .append(encoder.encode(shardState.getShardInstance().getHostName()));
        args.append("&").append(encoder.encode("template")).append("=")
                .append(encoder.encode(shardState.getShardInstance().getShard().getFloc().getTemplate()));

        for (String key : shardState.getShardInstance().getShard().getFloc().getPropertyBag().keySet()) {
            String value = shardState.getShardInstance().getShard().getFloc().getPropertyBag().get(key);
            if (value != null) {
                args.append("&").append(encoder.encode("floc.property." + key)).append("=")
                        .append(encoder.encode(value));
            }
        }

        for (String key : shardState.getPropertyBag().keySet()) {
            String value = shardState.getPropertyBag().get(key);
            if (value != null) {
                args.append("&").append(encoder.encode("state.property." + key)).append("=")
                        .append(encoder.encode(value));
            }
        }

        args.append("&").append(encoder.encode("instance")).append("=")
                .append(encoder.encode("" + shardState.getShardInstance().getShard().getInstance()));
        args.append("&").append(encoder.encode("numberOfShards")).append("=").append(
                encoder.encode("" + shardState.getShardInstance().getShard().getFloc().getNumberOfShards()));
        args.append("&").append(encoder.encode("port")).append("=")
                .append(encoder.encode("" + shardState.getShardInstance().getPort()));
        args.append("&").append(encoder.encode("stores")).append("=");
        for (StoreRef store : shardState.getShardInstance().getShard().getFloc().getStoreRefs()) {
            if (args.charAt(args.length() - 1) != '=') {
                args.append(encoder.encode(","));
            }
            args.append(encoder.encode(store.toString()));
        }
        args.append("&").append(encoder.encode("isMaster")).append("=")
                .append(encoder.encode("" + shardState.isMaster()));
        args.append("&").append(encoder.encode("hasContent")).append("=")
                .append(encoder.encode("" + shardState.getShardInstance().getShard().getFloc().hasContent()));
        args.append("&").append(encoder.encode("shardMethod")).append("=").append(
                encoder.encode(shardState.getShardInstance().getShard().getFloc().getShardMethod().toString()));

        args.append("&").append(encoder.encode("lastUpdated")).append("=")
                .append(encoder.encode("" + shardState.getLastUpdated()));
        args.append("&").append(encoder.encode("lastIndexedChangeSetCommitTime")).append("=")
                .append(encoder.encode("" + shardState.getLastIndexedChangeSetCommitTime()));
        args.append("&").append(encoder.encode("lastIndexedChangeSetId")).append("=")
                .append(encoder.encode("" + shardState.getLastIndexedChangeSetId()));
        args.append("&").append(encoder.encode("lastIndexedTxCommitTime")).append("=")
                .append(encoder.encode("" + shardState.getLastIndexedTxCommitTime()));
        args.append("&").append(encoder.encode("lastIndexedTxId")).append("=")
                .append(encoder.encode("" + shardState.getLastIndexedTxId()));

    }

    url.append(args);

    GetRequest req = new GetRequest(url.toString());
    Response response = null;
    List<Transaction> transactions = new ArrayList<Transaction>();
    Long maxTxnCommitTime = null;
    Long maxTxnIdOnServer = null;
    try {
        response = repositoryHttpClient.sendRequest(req);
        if (response.getStatus() != HttpStatus.SC_OK) {
            throw new AlfrescoRuntimeException("GetTransactions return status is " + response.getStatus());
        }

        Reader reader = new BufferedReader(new InputStreamReader(response.getContentAsStream(), "UTF-8"));
        JsonParser parser = jsonFactory.createJsonParser(reader);

        JsonToken token = parser.nextValue();
        while (token != null) {
            if ("transactions".equals(parser.getCurrentName())) {
                token = parser.nextToken(); //START_ARRAY
                while (token == JsonToken.START_OBJECT) {
                    token = parser.nextValue();
                    long id = parser.getLongValue();

                    token = parser.nextValue();
                    long commitTime = parser.getLongValue();

                    token = parser.nextValue();
                    long updates = parser.getLongValue();

                    token = parser.nextValue();
                    long deletes = parser.getLongValue();

                    Transaction txn = new Transaction();
                    txn.setCommitTimeMs(commitTime);
                    txn.setDeletes(deletes);
                    txn.setId(id);
                    txn.setUpdates(updates);

                    transactions.add(txn);

                    token = parser.nextToken(); //END_OBJECT
                    token = parser.nextToken(); // START_OBJECT or END_ARRAY;
                }
            } else if ("maxTxnCommitTime".equals(parser.getCurrentName())) {
                maxTxnCommitTime = parser.getLongValue();
            } else if ("maxTxnId".equals(parser.getCurrentName())) {
                maxTxnIdOnServer = parser.getLongValue();
            }
            token = parser.nextValue();
        }
        parser.close();
        reader.close();

    } finally {
        if (response != null) {
            response.release();
        }
    }

    return new Transactions(transactions, maxTxnCommitTime, maxTxnIdOnServer);
}