001/* 002 * Copyright 2002-2009 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.web.client.support; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021 022import org.springframework.http.client.ClientHttpRequestFactory; 023import org.springframework.util.Assert; 024import org.springframework.web.client.RestTemplate; 025 026/** 027 * Convenient super class for application classes that need REST access. 028 * 029 * <p>Requires a {@link ClientHttpRequestFactory} or a {@link RestTemplate} instance to be set. 030 * 031 * @author Arjen Poutsma 032 * @since 3.0 033 * @see #setRestTemplate 034 * @see org.springframework.web.client.RestTemplate 035 */ 036public class RestGatewaySupport { 037 038 /** Logger available to subclasses */ 039 protected final Log logger = LogFactory.getLog(getClass()); 040 041 private RestTemplate restTemplate; 042 043 044 /** 045 * Construct a new instance of the {@link RestGatewaySupport}, with default parameters. 046 */ 047 public RestGatewaySupport() { 048 this.restTemplate = new RestTemplate(); 049 } 050 051 /** 052 * Construct a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}. 053 * @see RestTemplate#RestTemplate(ClientHttpRequestFactory) 054 */ 055 public RestGatewaySupport(ClientHttpRequestFactory requestFactory) { 056 Assert.notNull(requestFactory, "'requestFactory' must not be null"); 057 this.restTemplate = new RestTemplate(requestFactory); 058 } 059 060 061 /** 062 * Sets the {@link RestTemplate} for the gateway. 063 */ 064 public void setRestTemplate(RestTemplate restTemplate) { 065 Assert.notNull(restTemplate, "'restTemplate' must not be null"); 066 this.restTemplate = restTemplate; 067 } 068 069 /** 070 * Returns the {@link RestTemplate} for the gateway. 071 */ 072 public RestTemplate getRestTemplate() { 073 return this.restTemplate; 074 } 075 076}