package fr.nikk.services.http;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Servlet implementation class Proxy
 */
public class Proxy extends HttpServlet {
	private static final long serialVersionUID = 6729550776774642774L;

	private final String forwardTo; 
	
	/**
     * @param uri The target uri
	 * @see HttpServlet#HttpServlet()
     */
    public Proxy(String uri) {
        super();
        this.forwardTo = uri;
    }
    
    @SuppressWarnings("resource")
    /**
     * method // TODO enum !
     *  0 : get
     *  1 : post
     *  2 : delete
     *  3 : put
     */
    private void forwardRequest(HttpServletRequest request, HttpServletResponse response, int method) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();

		// get request args
		String uri = request.getRequestURI();
		String r = "";
		if(uri.indexOf("/", 2) >= 0)
			r = uri.substring(uri.indexOf("/", 2), uri.length());
		
        // Second request
		String forwURI = this.forwardTo + "/" + r;
        HttpRequestBase httpreq = new HttpGet(forwURI);
        switch (method) {
			case 0:
				httpreq = new HttpGet(forwURI);
				break;
			case 1:
				httpreq = new HttpPost(forwURI);
				break;
			case 2:
				httpreq = new HttpDelete(forwURI);
				break;
			case 3:			
				httpreq = new HttpPut(forwURI);
				break;
			default:
				break;
		}
        CloseableHttpResponse response_get = httpclient.execute(httpreq);  
        HttpEntity entity_get = response_get.getEntity();            
        
        // write the response
        String resp = EntityUtils.toString(entity_get);
        
        PrintWriter writer = response.getWriter();
        writer.println(resp);
        
        httpclient.close();
        writer.close();
        response_get.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.forwardRequest(request, response, 0);
	}
    
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.forwardRequest(request, response, 2);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.forwardRequest(req, resp, 1);
	}
	
	@Override
	protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.forwardRequest(req, resp, 3);
	}

}
@@ -899,16 +899,80 @@
 2774L;%0A%0A
+%09private enum HttpMethods %7B%0A%09%09GET,%0A%09%09POST,%0A%09%09DELETE,%0A%09%09PUT%0A%09%7D%0A%09%0A
 %09private
@@ -997,16 +997,16 @@
 ardTo; %0A
-
 %09%0A%09/**%0A 
@@ -1207,121 +1207,8 @@
 e%22)%0A
-    /**%0A     * method // TODO enum !%0A     *  0 : get%0A     *  1 : post%0A     *  2 : delete%0A     *  3 : put%0A     */%0A
     
@@ -1293,19 +1293,27 @@
 sponse, 
-int
+HttpMethods
  method)
@@ -1397,17 +1397,21 @@
 fault();
-%0A
+%09%09%0A%09%09
 %0A%09%09// ge
@@ -1734,17 +1734,19 @@
 %09%09%09case 
-0
+GET
 :%0A%09%09%09%09ht
@@ -1794,17 +1794,20 @@
 %09%09%09case 
-1
+POST
 :%0A%09%09%09%09ht
@@ -1856,17 +1856,22 @@
 %09%09%09case 
-2
+DELETE
 :%0A%09%09%09%09ht
@@ -1922,17 +1922,19 @@
 %09%09%09case 
-3
+PUT
 :%09%09%09%0A%09%09%09
@@ -2596,17 +2596,31 @@
 sponse, 
-0
+HttpMethods.GET
 );%0A%09%7D%0A  
@@ -2785,17 +2785,34 @@
 sponse, 
-2
+HttpMethods.DELETE
 );%0A%09%7D%0A%09%0A
@@ -2971,9 +2971,24 @@
 sp, 
-1
+HttpMethods.POST
 );%0A%09
@@ -3150,9 +3150,23 @@
 sp, 
-3
+HttpMethods.PUT
 );%0A%09

Replace the crappy int by an enum