ServletRequestContextBuilder implements RequestContextBuilder { public SimpleRopRequestContext buildBySysParams(RopContext ropContext, Object request, Object response) { if (!(request instanceof HttpServletRequest)) { throw new IllegalArgumentException("请求对象必须是HttpServletRequest的类型"); } if(response != null && !(response instanceof HttpServletResponse)){ throw new IllegalArgumentException("请求对象必须是HttpServletResponse的类型"); } HttpServletRequest servletRequest = (HttpServletRequest) request; SimpleRopRequestContext requestContext = new SimpleRopRequestContext(ropContext); requestContext.setRawRequestObject(servletRequest); if (response != null) { requestContext.setRawResponseObject(response); } requestContext.setAllParams(getRequestParams(servletRequest)); requestContext.setIp(getRemoteAddr(servletRequest)); requestContext.setAppKey(servletRequest.getParameter(SystemParameterNames.getAppKey())); requestContext.setSessionId(servletRequest.getParameter(SystemParameterNames.getSessionId())); requestContext.setMethod(servletRequest.getParameter(SystemParameterNames.getMethod())); requestContext.setVersion(servletRequest.getParameter(SystemParameterNames.getVersion())); requestContext.setLocale(getLocale(servletRequest)); requestContext.setFormat(getFormat(servletRequest)); requestContext.setMessageFormat(getResponseFormat(servletRequest)); requestContext.setSign(servletRequest.getParameter(SystemParameterNames.getSign())); requestContext.setHttpAction(HttpAction.fromValue(servletRequest.getMethod())); ServiceMethodHandler serviceMethodHandler = ropContext.getServiceMethodHandler(requestContext.getMethod(), requestContext.getVersion()); requestContext.setServiceMethodHandler(serviceMethodHandler); return requestContext; } ServletRequestContextBuilder(FormattingConversionService conversionService); SimpleRopRequestContext buildBySysParams(RopContext ropContext,
                                                    Object request,
                                                    Object response); RopRequest buildRopRequest(RopRequestContext ropRequestContext); static Locale getLocale(HttpServletRequest webRequest); static MessageFormat getResponseFormat(HttpServletRequest servletRequest); FormattingConversionService getFormattingConversionService(); static final String X_REAL_IP; static final String X_FORWARDED_FOR; }
@Test public void testIpParsed() { FormattingConversionService conversionService = mock(FormattingConversionService.class); SessionManager sessionManager = mock(SessionManager.class); ServletRequestContextBuilder requestContextBuilder = new ServletRequestContextBuilder(conversionService); RopContext ropContext = mock(RopContext.class); MockHttpServletRequest servletRequest = new MockHttpServletRequest(); servletRequest.setRemoteAddr("1.1.1.1"); SimpleRopRequestContext requestContext = requestContextBuilder.buildBySysParams(ropContext, servletRequest,null); assertEquals(requestContext.getIp(), "1.1.1.1"); servletRequest.setRemoteAddr("1.1.1.1"); servletRequest.addHeader(ServletRequestContextBuilder.X_FORWARDED_FOR, "null,2.2.2.2,3.3.3.3"); requestContext = requestContextBuilder.buildBySysParams(ropContext, servletRequest,null); assertEquals(requestContext.getIp(), "2.2.2.2"); servletRequest.addHeader(ServletRequestContextBuilder.X_REAL_IP, "5.5.5.5"); requestContext = requestContextBuilder.buildBySysParams(ropContext, servletRequest,null); assertEquals(requestContext.getIp(), "5.5.5.5"); }
@Test(expectedExceptions = {IllegalArgumentException.class}) public void testBuildBySysParams3() throws Exception { FormattingConversionService conversionService = mock(FormattingConversionService.class); ServletRequestContextBuilder requestContextBuilder = new ServletRequestContextBuilder(conversionService); RopContext ropContext = mock(RopContext.class); ServiceMethodHandler methodHandler = mock(ServiceMethodHandler.class); when(ropContext.getServiceMethodHandler("method1", "3.0")).thenReturn(methodHandler); SimpleRopRequestContext requestContext = requestContextBuilder.buildBySysParams(ropContext, new Object(),null); }