Last active
July 27, 2016 22:10
-
-
Save pedrotoliveira/6f968a0e9d2a7d07d0c87e0bb6bd111e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.gov.sp.tce.audesp.controller.servlet; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Enumeration; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class RedirectServlet extends HttpServlet { | |
private static final long serialVersionUID = -9073728400053939150L; | |
private static final Logger logger = LoggerFactory.getLogger(RedirectServlet.class); | |
@Override | |
public void init() throws ServletException { | |
super.init(); | |
} | |
@Override | |
public void destroy() { | |
super.destroy(); | |
} | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
doPost(request, response); | |
} | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
forwardRequest("POST", request, response); | |
} | |
private void forwardRequest(String method, HttpServletRequest request, HttpServletResponse response) throws IOException { | |
final boolean hasoutbody = (method.equals("POST")); | |
try { | |
logger.info("Recebendo Pacote - [contexto]"); | |
String path = "http://localhost:8080" + "/contexto"; | |
String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : ""; | |
final URL url = new URL(path + queryString); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod(method); | |
final Enumeration<String> headers = request.getHeaderNames(); | |
while (headers.hasMoreElements()) { | |
final String header = headers.nextElement(); | |
final Enumeration<String> values = request.getHeaders(header); | |
while (values.hasMoreElements()) { | |
final String value = values.nextElement(); | |
conn.addRequestProperty(header, value); | |
} | |
} | |
//conn.setFollowRedirects(false); // throws AccessDenied exception | |
conn.setUseCaches(false); | |
conn.setDoInput(true); | |
conn.setDoOutput(hasoutbody); | |
logger.info("Redirecionando Para: " + url.toString()); | |
conn.connect(); | |
final byte[] buffer = new byte[16384]; | |
while (hasoutbody) { | |
final int read = request.getInputStream().read(buffer); | |
if (read <= 0) { | |
break; | |
} | |
conn.getOutputStream().write(buffer, 0, read); | |
} | |
response.setStatus(conn.getResponseCode()); | |
for (int i = 0;; ++i) { | |
final String header = conn.getHeaderFieldKey(i); | |
if (header == null) { | |
break; | |
} | |
final String value = conn.getHeaderField(i); | |
response.setHeader(header, value); | |
} | |
while (true) { | |
final int read = conn.getInputStream().read(buffer); | |
if (read <= 0) { | |
break; | |
} | |
response.getOutputStream().write(buffer, 0, read); | |
} | |
} catch (Exception e) { | |
logger.error("Erro no foward da request [contexto]", e); | |
} finally { | |
response.flushBuffer(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment