Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.modules.sys.web; import com.eryansky.common.model.Result; import com.eryansky.common.utils.StringUtils; import com.eryansky.common.utils.http.HttpCompoents; import com.eryansky.common.web.filter.CustomHttpServletRequestWrapper; import com.eryansky.common.web.springmvc.SimpleController; import com.eryansky.common.web.utils.WebUtils; import com.eryansky.core.security.annotation.RequiresUser; import com.eryansky.utils.AppConstants; import com.eryansky.utils.AppUtils; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.fluent.Response; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * ?? * @author &Eryan eryanwcp@gmail.com * @date 2015-12-14 */ @RequiresUser(required = false) @Controller @RequestMapping(value = "${adminPath}/proxy") public class ProxyController extends SimpleController { /** * ? * @param nativeWebRequest * @param contentUrl URL * @throws IOException */ @RequestMapping(value = { "" }) public void getProxy(NativeWebRequest nativeWebRequest, String contentUrl) throws Exception { CustomHttpServletRequestWrapper request = nativeWebRequest .getNativeRequest(CustomHttpServletRequestWrapper.class); HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class); HttpCompoents httpCompoents = HttpCompoents.getInstance();//?? ?Cookie? String param = AppUtils.joinParasWithEncodedValue(WebUtils.getParametersStartingWith(request, null));//? String url = contentUrl + "?" + param; logger.debug("proxy url{}", url); Response remoteResponse = httpCompoents.getResponse(url); try { // if (remoteResponse == null) { String errorMsg = "?" + contentUrl; logger.error(errorMsg); if (WebUtils.isAjaxRequest(request)) { WebUtils.renderJson(response, Result.errorResult().setObj(errorMsg)); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg); } return; } HttpResponse httpResponse = remoteResponse.returnResponse(); HttpEntity entity = httpResponse.getEntity(); // if (httpResponse.getStatusLine().getStatusCode() >= 400) { String errorMsg = "?" + contentUrl; logger.error(errorMsg); logger.error(httpResponse.getStatusLine().getStatusCode() + ""); logger.error(EntityUtils.toString(entity, "utf-8")); if (WebUtils.isAjaxRequest(request)) { WebUtils.renderJson(response, Result.errorResult().setObj(errorMsg)); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg); } return; } // Header response.setContentType(entity.getContentType().getValue()); if (entity.getContentLength() > 0) { response.setContentLength((int) entity.getContentLength()); } // InputStream input = entity.getContent(); OutputStream output = response.getOutputStream(); // byte?InputStreamOutputStream, ?4k. IOUtils.copy(input, output); output.flush(); } finally { } } /** * ? * @param nativeWebRequest * @throws IOException */ @RequestMapping(value = { "**" }) public ModelAndView proxy(NativeWebRequest nativeWebRequest) throws Exception { CustomHttpServletRequestWrapper request = nativeWebRequest .getNativeRequest(CustomHttpServletRequestWrapper.class); String requestUrl = request.getRequestURI(); String contentUrl = StringUtils.substringAfterLast(requestUrl, AppConstants.getAdminPath() + "/proxy/"); String param = AppUtils.joinParasWithEncodedValue(WebUtils.getParametersStartingWith(request, null));//? String url = contentUrl + "?" + param; logger.debug("proxy url{}", url); HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class); HttpCompoents httpCompoents = HttpCompoents.getInstance();//?? ?Cookie? Response remoteResponse = httpCompoents.getResponse(url); try { // if (remoteResponse == null) { String errorMsg = "?" + contentUrl; logger.error(errorMsg); if (WebUtils.isAjaxRequest(request)) { WebUtils.renderJson(response, Result.errorResult().setObj(errorMsg)); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg); } return null; } HttpResponse httpResponse = remoteResponse.returnResponse(); HttpEntity entity = httpResponse.getEntity(); // if (httpResponse.getStatusLine().getStatusCode() >= 400) { String errorMsg = "?" + contentUrl; logger.error(errorMsg); logger.error(httpResponse.getStatusLine().getStatusCode() + ""); logger.error(EntityUtils.toString(entity, "utf-8")); if (WebUtils.isAjaxRequest(request)) { WebUtils.renderJson(response, Result.errorResult().setObj(errorMsg)); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, errorMsg); } return null; } // Header response.setContentType(entity.getContentType().getValue()); if (entity.getContentLength() > 0) { response.setContentLength((int) entity.getContentLength()); } // InputStream input = entity.getContent(); OutputStream output = response.getOutputStream(); // byte?InputStreamOutputStream, ?4k. IOUtils.copy(input, output); output.flush(); } catch (IOException e) { e.printStackTrace(); } finally { } return null; } }