/*
 * Copyright 2014 JBoss Inc
 *
 * 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 io.ericwittmann.corsproxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import org.apache.commons.io.IOUtils;

/**
 * A proxy servlet used to get around cross-origin problems when pulling
 * down content from remote sites.
 *
 * @author eric.wittmann@redhat.com
 */
@SuppressWarnings("nls")
public class ProxyServlet extends HttpServlet {

    private static final long serialVersionUID = -4704803997251798191L;

    private static Set<String> EXCLUDE_HEADERS = new HashSet<>();
    static {
        EXCLUDE_HEADERS.add("ETag");
        EXCLUDE_HEADERS.add("Last-Modified");
        EXCLUDE_HEADERS.add("Date");
        EXCLUDE_HEADERS.add("Cache-control");
    }

    /**
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        String url = "https://issues.jboss.org" + req.getPathInfo();
        if (req.getQueryString() != null) {
            url += "?" + req.getQueryString();
        }

        System.out.println("Proxying to: " + url);

        URL remoteUrl = new URL(url);
        HttpURLConnection remoteConn = (HttpURLConnection) remoteUrl.openConnection();
        InputStream remoteIS = null;
        OutputStream responseOS = null;
        try {
            String auth = req.getHeader("Authorization");
            if (auth != null) {
                remoteConn.setRequestProperty("Authorization", auth);
            }
            remoteConn.connect();
            Map<String, List<String>> headerFields = remoteConn.getHeaderFields();
            for (String headerName : headerFields.keySet()) {
                if (headerName == null) {
                    continue;
                }
                if (EXCLUDE_HEADERS.contains(headerName)) {
                    continue;
                }
                String headerValue = remoteConn.getHeaderField(headerName);
                resp.setHeader(headerName, headerValue);
            }
            resp.setHeader("Cache-control", "no-cache, no-store, must-revalidate"); //$NON-NLS-2$
            remoteIS = remoteConn.getInputStream();
            responseOS = resp.getOutputStream();
            IOUtils.copy(remoteIS, responseOS);
            resp.flushBuffer();
        } catch (Exception e) {
            resp.sendError(500, e.getMessage());
        } finally {
            IOUtils.closeQuietly(responseOS);
            IOUtils.closeQuietly(remoteIS);
        }
    }

}

@@ -1674,21 +1674,23 @@
 Servlet#
-doGet
+service
 (javax.s
@@ -1806,13 +1806,15 @@
 oid 
-doGet
+service
 (Htt
@@ -2132,16 +2132,127 @@
  + url);
+%0A        boolean isWrite = req.getMethod().equalsIgnoreCase(%22post%22) %7C%7C req.getMethod().equalsIgnoreCase(%22put%22);
 %0A%0A      
@@ -2382,95 +2382,76 @@
     
-InputStream remoteIS = null;%0A        OutputStream responseOS = null;%0A        try %7B%0A    
+if (isWrite) %7B%0A            remoteConn.setDoOutput(true);%0A        %7D%0A%0A
     
@@ -2504,28 +2504,24 @@
 %22);%0A        
-    
 if (auth != 
@@ -2532,36 +2532,32 @@
 ) %7B%0A            
- 
-   
 remoteConn.setRe
@@ -2606,47 +2606,714 @@
     
+%7D%0A%0A
     
-%7D%0A            remoteConn.connect();
+    if (isWrite) %7B%0A            InputStream requestIS = null;%0A            OutputStream remoteOS = null;%0A            try %7B%0A                requestIS = req.getInputStream();%0A                remoteOS = remoteConn.getOutputStream();%0A                IOUtils.copy(requestIS, remoteOS);%0A                remoteOS.flush();%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A                resp.sendError(500, e.getMessage());%0A                return;%0A            %7D finally %7B%0A                IOUtils.closeQuietly(requestIS);%0A                IOUtils.closeQuietly(remoteOS);%0A            %7D%0A        %7D%0A%0A        InputStream remoteIS = null;%0A        OutputStream responseOS = null;%0A        try %7B
 %0A   
@@ -4102,32 +4102,65 @@
 (Exception e) %7B%0A
+            e.printStackTrace();%0A
             resp

support other methods