<commit_before>package com.greendelta.cloud.util;

import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;

public class WebRequests {

	private static final Logger log = LoggerFactory.getLogger(WebRequests.class);

	public static ClientResponse call(Type type, String url, String sessionId) throws WebRequestException {
		return call(type, url, sessionId, null);
	}

	public static ClientResponse call(Type type, String url, String sessionId, Object data) throws WebRequestException {
		log.info(Strings.concat("Calling ", url, " with type " + type.name()));
		Client client = Client.create();
		WebResource resource = client.resource(url);
		Builder responseBuilder = resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE);
		if (sessionId != null)
			responseBuilder.cookie(new Cookie("JSESSIONID", sessionId));
		if (data != null)
			responseBuilder.entity(new Gson().toJson(data), MediaType.APPLICATION_JSON_TYPE);
		ClientResponse response = null;
		switch (type) {
		case GET:
			response = responseBuilder.get(ClientResponse.class);
			break;
		case POST:
			response = responseBuilder.post(ClientResponse.class);
			break;
		case PUT:
			response = responseBuilder.put(ClientResponse.class);
			break;
		case DELETE:
			response = responseBuilder.delete(ClientResponse.class);
			break;
		}
		if (response.getStatus() >= 400 && response.getStatus() <= 599)
			throw new WebRequestException(response);
		return response;
	}

	public static enum Type {
		GET, POST, PUT, DELETE;
	}

	public static class WebRequestException extends Exception {

		private static final long serialVersionUID = 1423557937866180113L;
		private int errorCode;<commit_msg>added support for input streams in webrequest helper<commit_after>package com.greendelta.cloud.util;

import java.io.InputStream;

import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;

public class WebRequests {

	private static final Logger log = LoggerFactory
			.getLogger(WebRequests.class);

	public static ClientResponse call(Type type, String url, String sessionId)
			throws WebRequestException {
		return call(type, url, sessionId, null);
	}

	public static ClientResponse call(Type type, String url, String sessionId,
			Object data) throws WebRequestException {
		log.info(Strings.concat("Calling ", url, " with type " + type.name()));
		Client client = Client.create();
		WebResource resource = client.resource(url);
		Builder responseBuilder = resource.accept(
				MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE);
		if (sessionId != null)
			responseBuilder.cookie(new Cookie("JSESSIONID", sessionId));
		if (data != null)
			if (data instanceof InputStream)
				responseBuilder.entity(data,
						MediaType.APPLICATION_OCTET_STREAM_TYPE);
			else
				responseBuilder.entity(new Gson().toJson(data),
						MediaType.APPLICATION_JSON_TYPE);
		ClientResponse response = null;
		switch (type) {
		case GET:
			response = responseBuilder.get(ClientResponse.class);
			break;
		case POST:
			response = responseBuilder.post(ClientResponse.class);
			break;
		case PUT:
			response = responseBuilder.put(ClientResponse.class);
			break;
		case DELETE:
			response = responseBuilder.delete(ClientResponse.class);
			break;
		}
		if (response.getStatus() >= 400 && response.getStatus() <= 599)
			throw new WebRequestException(response);
		return response;
	}

	public static enum Type {
		GET, POST, PUT, DELETE;
	}

	public static class WebRequestException extends Exception {

		private static final long serialVersionUID = 1423557937866180113L;
		private int errorCode;
{'fraction_non_alphanumeric': 0.10243432152325861, 'fraction_numerical': 0.013015184381778741, 'mean_word_length': 4.878186968838527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '139059', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1236, 'n_words': 303}