View Javadoc
1   package org.apache.maven.wagon.shared.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.http.Header;
23  import org.apache.http.message.BasicHeader;
24  import org.apache.maven.wagon.Wagon;
25  
26  import java.util.Map;
27  import java.util.Properties;
28  
29  /**
30   *
31   */
32  public class HttpMethodConfiguration
33  {
34  
35      private Boolean useDefaultHeaders;
36  
37      private Properties headers = new Properties();
38  
39      private Properties params = new Properties();
40  
41      private int connectionTimeout = Wagon.DEFAULT_CONNECTION_TIMEOUT;
42  
43      private int readTimeout =
44          Integer.parseInt( System.getProperty( "maven.wagon.rto", Integer.toString( Wagon.DEFAULT_READ_TIMEOUT ) ) );
45  
46      private boolean usePreemptive = false;
47  
48      public boolean isUseDefaultHeaders()
49      {
50          return useDefaultHeaders == null || useDefaultHeaders.booleanValue();
51      }
52  
53      public HttpMethodConfiguration setUseDefaultHeaders( boolean useDefaultHeaders )
54      {
55          this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders );
56          return this;
57      }
58  
59      public Boolean getUseDefaultHeaders()
60      {
61          return useDefaultHeaders;
62      }
63  
64      public HttpMethodConfiguration addHeader( String header, String value )
65      {
66          headers.setProperty( header, value );
67          return this;
68      }
69  
70      public Properties getHeaders()
71      {
72          return headers;
73      }
74  
75      public HttpMethodConfiguration setHeaders( Properties headers )
76      {
77          this.headers = headers;
78          return this;
79      }
80  
81      public HttpMethodConfiguration addParam( String param, String value )
82      {
83          params.setProperty( param, value );
84          return this;
85      }
86  
87      public Properties getParams()
88      {
89          return params;
90      }
91  
92      public HttpMethodConfiguration setParams( Properties params )
93      {
94          this.params = params;
95          return this;
96      }
97  
98      public int getConnectionTimeout()
99      {
100         return connectionTimeout;
101     }
102 
103     public HttpMethodConfiguration setConnectionTimeout( int connectionTimeout )
104     {
105         this.connectionTimeout = connectionTimeout;
106         return this;
107     }
108 
109     public int getReadTimeout()
110     {
111         return readTimeout;
112     }
113 
114     public HttpMethodConfiguration setReadTimeout( int readTimeout )
115     {
116         this.readTimeout = readTimeout;
117         return this;
118     }
119 
120     public boolean isUsePreemptive()
121     {
122         return usePreemptive;
123     }
124 
125     public HttpMethodConfiguration setUsePreemptive( boolean usePreemptive )
126     {
127         this.usePreemptive = usePreemptive;
128         return this;
129     }
130 
131     public Header[] asRequestHeaders()
132     {
133         if ( headers == null )
134         {
135             return new Header[0];
136         }
137 
138         Header[] result = new Header[headers.size()];
139 
140         int index = 0;
141         for ( Map.Entry entry : headers.entrySet() )
142         {
143             String key = (String) entry.getKey();
144             String value = (String) entry.getValue();
145 
146             Header header = new BasicHeader( key, value );
147             result[index++] = header;
148         }
149 
150         return result;
151     }
152 
153     HttpMethodConfiguration copy()
154     {
155         HttpMethodConfiguration copy = new HttpMethodConfiguration();
156 
157         copy.setConnectionTimeout( getConnectionTimeout() );
158         copy.setReadTimeout( getReadTimeout() );
159         if ( getHeaders() != null )
160         {
161             copy.getHeaders().putAll( getHeaders() );
162         }
163 
164         if ( getParams() != null )
165         {
166             copy.getParams().putAll( getParams() );
167         }
168 
169         copy.setUseDefaultHeaders( isUseDefaultHeaders() );
170 
171         return copy;
172     }
173 
174 }