001/*
002 * Copyright 2002-2017 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.AsyncClientHttpRequest;
027import org.springframework.http.client.AsyncClientHttpRequestFactory;
028import org.springframework.util.Assert;
029
030/**
031 * Base class for {@link org.springframework.web.client.AsyncRestTemplate}
032 * and other HTTP accessing gateway helpers, defining common properties
033 * such as the {@link AsyncClientHttpRequestFactory} to operate on.
034 *
035 * <p>Not intended to be used directly. See
036 * {@link org.springframework.web.client.AsyncRestTemplate}.
037 *
038 * @author Arjen Poutsma
039 * @since 4.0
040 * @see org.springframework.web.client.AsyncRestTemplate
041 */
042public class AsyncHttpAccessor {
043
044        /** Logger available to subclasses. */
045        protected final Log logger = LogFactory.getLog(getClass());
046
047        private AsyncClientHttpRequestFactory asyncRequestFactory;
048
049
050        /**
051         * Set the request factory that this accessor uses for obtaining {@link
052         * org.springframework.http.client.ClientHttpRequest HttpRequests}.
053         */
054        public void setAsyncRequestFactory(AsyncClientHttpRequestFactory asyncRequestFactory) {
055                Assert.notNull(asyncRequestFactory, "AsyncClientHttpRequestFactory must not be null");
056                this.asyncRequestFactory = asyncRequestFactory;
057        }
058
059        /**
060         * Return the request factory that this accessor uses for obtaining {@link
061         * org.springframework.http.client.ClientHttpRequest HttpRequests}.
062         */
063        public AsyncClientHttpRequestFactory getAsyncRequestFactory() {
064                return this.asyncRequestFactory;
065        }
066
067        /**
068         * Create a new {@link AsyncClientHttpRequest} via this template's
069         * {@link AsyncClientHttpRequestFactory}.
070         * @param url the URL to connect to
071         * @param method the HTTP method to execute (GET, POST, etc.)
072         * @return the created request
073         * @throws IOException in case of I/O errors
074         */
075        protected AsyncClientHttpRequest createAsyncRequest(URI url, HttpMethod method) throws IOException {
076                AsyncClientHttpRequest request = getAsyncRequestFactory().createAsyncRequest(url, method);
077                if (logger.isDebugEnabled()) {
078                        logger.debug("Created asynchronous " + method.name() + " request for \"" + url + "\"");
079                }
080                return request;
081        }
082
083}