| | |
| | | package com.moon.server.helper; |
| | | |
| | | import com.moon.server.entity.all.StaticData; |
| | | import com.moon.server.entity.sys.ResEntity; |
| | | import org.apache.http.*; |
| | | import org.apache.http.client.config.CookieSpecs; |
| | | import org.apache.http.client.config.RequestConfig; |
| | | import org.apache.http.client.utils.URIUtils; |
| | | import org.apache.http.entity.InputStreamEntity; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicHeader; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.PrintWriter; |
| | | import java.net.HttpCookie; |
| | | import java.net.URI; |
| | | import java.net.URISyntaxException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Enumeration; |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | public void service(HttpServletRequest request, HttpServletResponse response, String url, String cookie) throws ServletException, IOException { |
| | | public void service(HttpServletRequest request, HttpServletResponse response, ResEntity entity, String url) throws ServletException, IOException { |
| | | HttpRequest proxyRequest; |
| | | if (request.getHeader(HttpHeaders.CONTENT_LENGTH) != null || request.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) { |
| | | proxyRequest = newProxyRequestWithEntity(request, url); |
| | |
| | | // copyRequestHeaders(request, proxyRequest, host); |
| | | //setXrForwardedForHeader(request, proxyRequest); |
| | | |
| | | if (!StringHelper.isEmpty(cookie)) { |
| | | proxyRequest.addHeader("Cookie", cookie + "; "); |
| | | } |
| | | //if (!StringHelper.isEmpty(cookie)) { |
| | | // proxyRequest.addHeader("Cookie", cookie + "; "); |
| | | //} |
| | | |
| | | CloseableHttpClient client = null; |
| | | HttpResponse proxyResponse = null; |
| | |
| | | if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { |
| | | response.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); |
| | | } else { |
| | | copyResponseEntity(proxyResponse, response); |
| | | copyResponseEntity(proxyResponse, request, response, entity); |
| | | } |
| | | } catch (Exception ex) { |
| | | throw new ServletException(ex.getMessage()); |
| | |
| | | return theUrl; |
| | | } |
| | | |
| | | protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse response) throws IOException { |
| | | protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletRequest request, HttpServletResponse response, ResEntity res) throws IOException { |
| | | HttpEntity entity = proxyResponse.getEntity(); |
| | | if (entity != null) { |
| | | //String str = EntityUtils.toString(entity, "UTF-8") |
| | | OutputStream servletOutputStream = response.getOutputStream(); |
| | | entity.writeTo(servletOutputStream); |
| | | if (null == entity) { |
| | | return; |
| | | } |
| | | |
| | | OutputStream outputStream = response.getOutputStream(); |
| | | |
| | | Header[] headers = proxyResponse.getHeaders("content-type"); |
| | | if (null != headers && headers.length > 0 && headers[0].getValue().contains(StaticData.TEXT_XML)) { |
| | | String str = EntityUtils.toString(entity, "UTF-8"); |
| | | if (str.contains(res.getUrl())) { |
| | | String proxyUrl = res.getProxy().replace("{token}", response.getHeader("token")); |
| | | proxyUrl = request.getRequestURL().substring(0, request.getRequestURL().indexOf(proxyUrl) + proxyUrl.length()); |
| | | str = str.replace(res.getUrl(), proxyUrl); |
| | | |
| | | byte[] bytes = str.getBytes(StandardCharsets.UTF_8); |
| | | response.setContentLength(bytes.length); |
| | | outputStream.write(bytes); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | entity.writeTo(outputStream); |
| | | } |
| | | } |