// Path: src/main/java/org/mockenize/exception/ProxyPathException.java
// public class ProxyPathException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 7714442933981404781L;
// 
// 	public ProxyPathException() {
//         super("Can't define proxy path.", Response.Status.INTERNAL_SERVER_ERROR);
//     }
// }
// 
// Path: src/main/java/org/mockenize/model/ProxyBean.java
// @Data
// public class ProxyBean implements Serializable, Cacheable {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	private static final String UNDERLINE = "_";
// 
// 	private static final String SLASH = "/";
// 
// 	@NotEmpty
// 	private String key;
// 
// 	@NotEmpty
// 	private String path;
// 
// 	@NotNull
// 	private String name;
// 
// 	@NotNull
// 	private URI url;
// 
// 	@Override
// 	public String getKey() {
// 		return path.toLowerCase().replaceAll(SLASH, UNDERLINE);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/repository/ProxyRepository.java
// @Repository
// public class ProxyRepository extends AbstractRepository<ProxyBean> {
// 
// 	private static final String CACHE_KEY = "proxies";
// 
// 	@Autowired
// 	public ProxyRepository(HazelcastInstance hazelcastInstance) {
// 		super(hazelcastInstance, CACHE_KEY);
// 	}
// 
// }


import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mockenize.exception.ProxyPathException;
import org.mockenize.model.ProxyBean;
import org.mockenize.repository.ProxyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Strings;
				if(deleted != null) {
					deletedProxies.add(deleted);
				}
			}
		} else {
			deletedProxies = proxyRepository.deleteAll();
		}
		return deletedProxies;
	}
	
	@Override
	public Response getResponse(HttpServletRequest request, JsonNode requestBody) {
		String path = request.getRequestURI();
		ProxyBean proxyBean = proxyRepository.findByKey(path);
		Builder requestBuilder = client.target(proxyBean.getUrl()).path(getRequestPath(proxyBean.getPath(), path))
				.request();
		addHeaders(request, requestBuilder);
		Entity<JsonNode> entity = Entity.entity(requestBody, getContentType(request));
		Response response = requestBuilder.build(request.getMethod(), entity).invoke();
		response.bufferEntity();
		return response;
	}

	private String getRequestPath(String path, String requestedPath) {
		if (path.matches("/.+")) {
			Pattern pattern = Pattern.compile("/[^/\\.]+(.*)");
			Matcher matcher = pattern.matcher(requestedPath);
			if (matcher.matches()) {
				return matcher.group(1);
			} else {
// Path: src/main/java/org/mockenize/exception/ProxyPathException.java
// public class ProxyPathException extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 7714442933981404781L;
// 
// 	public ProxyPathException() {
//         super("Can't define proxy path.", Response.Status.INTERNAL_SERVER_ERROR);
//     }
// }
// 
// Path: src/main/java/org/mockenize/model/ProxyBean.java
// @Data
// public class ProxyBean implements Serializable, Cacheable {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	private static final String UNDERLINE = "_";
// 
// 	private static final String SLASH = "/";
// 
// 	@NotEmpty
// 	private String key;
// 
// 	@NotEmpty
// 	private String path;
// 
// 	@NotNull
// 	private String name;
// 
// 	@NotNull
// 	private URI url;
// 
// 	@Override
// 	public String getKey() {
// 		return path.toLowerCase().replaceAll(SLASH, UNDERLINE);
// 	}
// }
// 
// Path: src/main/java/org/mockenize/repository/ProxyRepository.java
// @Repository
// public class ProxyRepository extends AbstractRepository<ProxyBean> {
// 
// 	private static final String CACHE_KEY = "proxies";
// 
// 	@Autowired
// 	public ProxyRepository(HazelcastInstance hazelcastInstance) {
// 		super(hazelcastInstance, CACHE_KEY);
// 	}
// 
// }


// Path: src/main/java/org/mockenize/service/ProxyService.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mockenize.exception.ProxyPathException;
import org.mockenize.model.ProxyBean;
import org.mockenize.repository.ProxyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Strings;

				if(deleted != null) {
					deletedProxies.add(deleted);
				}
			}
		} else {
			deletedProxies = proxyRepository.deleteAll();
		}
		return deletedProxies;
	}
	
	@Override
	public Response getResponse(HttpServletRequest request, JsonNode requestBody) {
		String path = request.getRequestURI();
		ProxyBean proxyBean = proxyRepository.findByKey(path);
		Builder requestBuilder = client.target(proxyBean.getUrl()).path(getRequestPath(proxyBean.getPath(), path))
				.request();
		addHeaders(request, requestBuilder);
		Entity<JsonNode> entity = Entity.entity(requestBody, getContentType(request));
		Response response = requestBuilder.build(request.getMethod(), entity).invoke();
		response.bufferEntity();
		return response;
	}

	private String getRequestPath(String path, String requestedPath) {
		if (path.matches("/.+")) {
			Pattern pattern = Pattern.compile("/[^/\\.]+(.*)");
			Matcher matcher = pattern.matcher(requestedPath);
			if (matcher.matches()) {
				return matcher.group(1);
			} else {
				throw new ProxyPathException();