/** * GET 요청을 처리한다. * * @param baseUrl 기본 URL * @param httpInvocation 기본 URL뒤에 붙는 경로 * @return */ public <T> T get(String baseUrl, HttpInvocation<T> httpInvocation) { Assert.notNull(httpInvocation, "httpInvocation 값은 null일 수 없다."); GetMethod getMethod = new GetMethod(baseUrl + httpInvocation.getUri()); if (httpInvocation.getParameters().length > 0) { getMethod.setQueryString(httpInvocation.getParameters()); } return invoke(getMethod, httpInvocation); }
protected <T> void processOnResponses(HttpMethod httpMethod, HttpInvocation<T> httpInvocation) throws Exception { int statusCode = httpMethod.getStatusCode(); httpInvocation.onResponseStatus(statusCode); Header[] responseHeaders = httpMethod.getResponseHeaders(); if (ArrayUtils.isNotEmpty(responseHeaders)) { for (Header header : responseHeaders) { httpInvocation.onResponseHeader(header.getName(), header.getValue()); } } httpInvocation.onResponseBody(httpMethod.getResponseBodyAsStream()); }
/** * POST 요청을 처리한다. * * @param <T> * @param baseUrl * @param httpInvocation * @return */ public <T> T post(String baseUrl, HttpInvocation<T> httpInvocation) { Assert.notNull(httpInvocation, "httpInvocation 값은 null일 수 없다."); PostMethod postMethod = new PostMethod(baseUrl + httpInvocation.getUri()); postMethod.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=" + httpInvocation.getEncoding()); String parameterEncoding = httpInvocation.getParameterEncoding(); if (StringUtils.isNotBlank(parameterEncoding)) { postMethod.getParams().setContentCharset(parameterEncoding); } postMethod.addParameters(httpInvocation.getParameters()); return invoke(postMethod, httpInvocation); }
/** * HTTP 요청 결과를 HttpInvocation 객체에 위임하여 결과를 가공해 리턴한다. * * @param <T> * @param httpMethod * @param httpInvocation * @return */ public <T> T invoke(HttpMethod httpMethod, HttpInvocation<T> httpInvocation) { populateCookiePolicy(httpMethod); populateRequestHeaders(httpMethod, httpInvocation); try { httpClient.executeMethod(httpMethod); processOnResponses(httpMethod, httpInvocation); } catch (Exception ex) { httpInvocation.onException(ex); } finally { httpMethod.releaseConnection(); } return httpInvocation.getReturnValue(); }
protected <T> void populateRequestHeaders( HttpMethod httpMethod, HttpInvocation<T> httpInvocation) { List<Header> requestHeaders = httpInvocation.getRequestHeaders(); if (!CollectionUtils.isEmpty(requestHeaders)) { for (Header header : requestHeaders) { httpMethod.addRequestHeader(header); } } }