<commit_before>package com.adaptris.jaxrscp.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import com.adaptris.jaxrscp.NameValuePair;
import com.adaptris.jaxrscp.WebTargetVisitor;
import com.adaptris.jaxrscp.reflections.EntityDescription;
import com.adaptris.jaxrscp.reflections.MetaDataReader;
import com.adaptris.jaxrscp.reflections.MetaDataReaderFactory;
import com.google.common.base.Optional;

public class ResourceHandler implements InvocationHandler{
	
	private final WebTarget target;
	private final MultivaluedMap<String, Object> headers;
	private final Class<?> resourceClass;
	private final MetaDataReaderFactory factory;

	ResourceHandler(Class<?> resourceClass, WebTarget target, MultivaluedMap<String, Object> headers, MetaDataReaderFactory factory) {
		this.target = target;
		this.headers = headers;
		this.resourceClass = resourceClass;
		this.factory = factory;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		MetaDataReader reader = this.factory.readerFor(this.resourceClass, method);
		WebTargetVisitor visitor = new WebTargetVisitor(reader);
		WebTarget target = visitor.visit(this.target, args);
		
		Builder request = target.request();
		request.headers(readHeaders(reader, args));
		
		String httpMethod = reader.readHttpMethod().or("GET");
		Optional<EntityDescription> entityDescription = reader.readEntity(args);
		Object result;
		if (entityDescription.isPresent()) {
			EntityDescription description = entityDescription.get();
			Object entity = description.getValue();
			if (description.getType() instanceof ParameterizedType) {				
				entity = new GenericEntity(entity, description.getType());
			}
            result = request.method(httpMethod, Entity.entity(entity, readContentType(request, reader)), reader.readResponseType());
		} else {
			result = request.method(httpMethod, reader.readResponseType());
		}		
		return result;
	}

	private String readContentType(Builder request, MetaDataReader reader) {<commit_msg>CPP-165 #comment removed redundant method param<commit_after>package com.adaptris.jaxrscp.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

import com.adaptris.jaxrscp.NameValuePair;
import com.adaptris.jaxrscp.WebTargetVisitor;
import com.adaptris.jaxrscp.reflections.EntityDescription;
import com.adaptris.jaxrscp.reflections.MetaDataReader;
import com.adaptris.jaxrscp.reflections.MetaDataReaderFactory;
import com.google.common.base.Optional;

public class ResourceHandler implements InvocationHandler{
	
	private final WebTarget target;
	private final MultivaluedMap<String, Object> headers;
	private final Class<?> resourceClass;
	private final MetaDataReaderFactory factory;

	ResourceHandler(Class<?> resourceClass, WebTarget target, MultivaluedMap<String, Object> headers, MetaDataReaderFactory factory) {
		this.target = target;
		this.headers = headers;
		this.resourceClass = resourceClass;
		this.factory = factory;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		MetaDataReader reader = this.factory.readerFor(this.resourceClass, method);
		WebTargetVisitor visitor = new WebTargetVisitor(reader);
		WebTarget target = visitor.visit(this.target, args);
		
		Builder request = target.request();
		request.headers(readHeaders(reader, args));
		
		String httpMethod = reader.readHttpMethod().or("GET");
		Optional<EntityDescription> entityDescription = reader.readEntity(args);
		Object result;
		if (entityDescription.isPresent()) {
			EntityDescription description = entityDescription.get();
			Object entity = description.getValue();
			if (description.getType() instanceof ParameterizedType) {				
				entity = new GenericEntity(entity, description.getType());
			}
            result = request.method(httpMethod, Entity.entity(entity, readContentType(reader)), reader.readResponseType());
		} else {
			result = request.method(httpMethod, reader.readResponseType());
		}		
		return result;
	}

	private String readContentType(MetaDataReader reader) {
{'fraction_non_alphanumeric': 0.10416666666666667, 'fraction_numerical': 0.0006127450980392157, 'mean_word_length': 5.782548476454294, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2560266', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1377, 'n_words': 298}