<commit_before>/*
 * Copyright 2015 <NAME> <<EMAIL>>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.graphity.core.client;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.update.UpdateRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.uri.UriComponent;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
import org.graphity.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class SPARQLClient
{

    private static final Logger log = LoggerFactory.getLogger(SPARQLClient.class);
    
    private final WebResource webResource;
    private final int maxGetRequestSize;

    protected SPARQLClient(WebResource webResource, int maxGetRequestSize)
    {
        this.webResource = webResource;
        this.maxGetRequestSize = maxGetRequestSize;
    }
    
    public WebResource getWebResource()
    {
        return webResource;
    }

    public static SPARQLClient create(WebResource webResource)
    {
        return new SPARQLClient(webResource, 8192);
    }

    public static SPARQLClient create(WebResource webResource, int maxGetRequestSize)
    {
        return new SPARQLClient(webResource, maxGetRequestSize);
    }
        
    public WebResource.Builder queryBuilder(Query query, javax.ws.rs.core.MediaType[] acceptedTypes, MultivaluedMap<String, String> params, int maxGetRequestSize)
    {
	if (log.isDebugEnabled()) log.debug("Remote service {} Query: {}", getWebResource().getURI(), query);
	if (query == null) throw new IllegalArgumentException("Query must be not null");
	if (acceptedTypes == null) throw new IllegalArgumentException("Accepted MediaType[] must be not null");

        // POST if request size is over limit, GET otherwise        
        if (query.toString().length() > maxGetRequestSize)
        {
            MultivaluedMap formData = new MultivaluedMapImpl();
            if (params != null) formData.putAll(params);
            formData.putSingle("query", query.toString());

            return getWebResource().accept(acceptedTypes).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        }
        else
        {
            // workaround for Jersey UriBuilder to encode { } brackets using UNRESERVED type
            String escapedQueryString = UriComponent.encode(query.toString(), UriComponent.Type.UNRESERVED);
            WebResource queryResource = getWebResource().queryParam("query", escapedQueryString);
            if (params != null)
            {
                MultivaluedMap<String, String> encodedParams = new MultivaluedMapImpl();
                for (Map.Entry<String, List<String>> entry : params.entrySet())
                    if (!entry.getKey().equals("query")) // query param is handled separately
                        for (String value : entry.getValue())
                            encodedParams.add(UriComponent.encode(entry.getKey(), UriComponent.Type.UNRESERVED),<commit_msg>Added missing SPARQLClient.queryBuilder() method variant<commit_after>/*
 * Copyright 2015 <NAME> <<EMAIL>>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.graphity.core.client;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.update.UpdateRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.uri.UriComponent;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
import org.graphity.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class SPARQLClient
{

    private static final Logger log = LoggerFactory.getLogger(SPARQLClient.class);
    
    private final WebResource webResource;
    private final int maxGetRequestSize;

    protected SPARQLClient(WebResource webResource, int maxGetRequestSize)
    {
        this.webResource = webResource;
        this.maxGetRequestSize = maxGetRequestSize;
    }
    
    public WebResource getWebResource()
    {
        return webResource;
    }

    public static SPARQLClient create(WebResource webResource)
    {
        return new SPARQLClient(webResource, 8192);
    }

    public static SPARQLClient create(WebResource webResource, int maxGetRequestSize)
    {
        return new SPARQLClient(webResource, maxGetRequestSize);
    }

    public WebResource.Builder queryBuilder(Query query, javax.ws.rs.core.MediaType[] acceptedTypes, MultivaluedMap<String, String> params)
    {
        return queryBuilder(query, acceptedTypes, params, getMaxGetRequestSize());
    }
    
    public WebResource.Builder queryBuilder(Query query, javax.ws.rs.core.MediaType[] acceptedTypes, MultivaluedMap<String, String> params, int maxGetRequestSize)
    {
	if (log.isDebugEnabled()) log.debug("Remote service {} Query: {}", getWebResource().getURI(), query);
	if (query == null) throw new IllegalArgumentException("Query must be not null");
	if (acceptedTypes == null) throw new IllegalArgumentException("Accepted MediaType[] must be not null");

        // POST if request size is over limit, GET otherwise        
        if (query.toString().length() > maxGetRequestSize)
        {
            MultivaluedMap formData = new MultivaluedMapImpl();
            if (params != null) formData.putAll(params);
            formData.putSingle("query", query.toString());

            return getWebResource().accept(acceptedTypes).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        }
        else
        {
            // workaround for Jersey UriBuilder to encode { } brackets using UNRESERVED type
            String escapedQueryString = UriComponent.encode(query.toString(), UriComponent.Type.UNRESERVED);
            WebResource queryResource = getWebResource().queryParam("query", escapedQueryString);
            if (params != null)
            {
                MultivaluedMap<String, String> encodedParams = new MultivaluedMapImpl();
                for (Map.Entry<String, List<String>> entry : params.entrySet())
                    if (!entry.getKey().equals("query")) // query param is handled separately
                        for (String value : entry.getValue())
                            encodedParams.add(UriComponent.encode(entry.getKey(), UriComponent.Type.UNRESERVED),
{'fraction_non_alphanumeric': 0.09767441860465116, 'fraction_numerical': 0.0037209302325581397, 'mean_word_length': 3.276136363636364, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4226808', 'n_tokens_mistral': 2066, 'n_tokens_neox': 2016, 'n_words': 559}