001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.http.client.support; 018 019import java.io.IOException; 020import java.net.URI; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025import org.springframework.http.HttpMethod; 026import org.springframework.http.client.ClientHttpRequest; 027import org.springframework.http.client.ClientHttpRequestFactory; 028import org.springframework.http.client.SimpleClientHttpRequestFactory; 029import org.springframework.util.Assert; 030 031/** 032 * Base class for {@link org.springframework.web.client.RestTemplate} 033 * and other HTTP accessing gateway helpers, defining common properties 034 * such as the {@link ClientHttpRequestFactory} to operate on. 035 * 036 * <p>Not intended to be used directly. 037 * See {@link org.springframework.web.client.RestTemplate}. 038 * 039 * @author Arjen Poutsma 040 * @author Juergen Hoeller 041 * @since 3.0 042 * @see org.springframework.web.client.RestTemplate 043 */ 044public abstract class HttpAccessor { 045 046 /** Logger available to subclasses */ 047 protected final Log logger = LogFactory.getLog(getClass()); 048 049 private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); 050 051 052 /** 053 * Set the request factory that this accessor uses for obtaining client request handles. 054 * <p>The default is a {@link SimpleClientHttpRequestFactory} based on the JDK's own 055 * HTTP libraries ({@link java.net.HttpURLConnection}). 056 * <p><b>Note that the standard JDK HTTP library does not support the HTTP PATCH method. 057 * Configure the Apache HttpComponents or OkHttp request factory to enable PATCH.</b> 058 * @see #createRequest(URI, HttpMethod) 059 * @see org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory 060 * @see org.springframework.http.client.OkHttp3ClientHttpRequestFactory 061 */ 062 public void setRequestFactory(ClientHttpRequestFactory requestFactory) { 063 Assert.notNull(requestFactory, "ClientHttpRequestFactory must not be null"); 064 this.requestFactory = requestFactory; 065 } 066 067 /** 068 * Return the request factory that this accessor uses for obtaining client request handles. 069 */ 070 public ClientHttpRequestFactory getRequestFactory() { 071 return this.requestFactory; 072 } 073 074 075 /** 076 * Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}. 077 * @param url the URL to connect to 078 * @param method the HTTP method to execute (GET, POST, etc) 079 * @return the created request 080 * @throws IOException in case of I/O errors 081 * @see #getRequestFactory() 082 * @see ClientHttpRequestFactory#createRequest(URI, HttpMethod) 083 */ 084 protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException { 085 ClientHttpRequest request = getRequestFactory().createRequest(url, method); 086 if (logger.isDebugEnabled()) { 087 logger.debug("Created " + method.name() + " request for \"" + url + "\""); 088 } 089 return request; 090 } 091 092}