View Javadoc
1   package org.apache.maven.wagon.providers.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 java.util.Map;
23  import java.util.Properties;
24  
25  import org.apache.http.Header;
26  import org.apache.http.message.BasicHeader;
27  import org.apache.maven.wagon.Wagon;
28  
29  public class HttpMethodConfiguration
30  {
31  
32      private Boolean useDefaultHeaders;
33  
34      private Properties headers = new Properties();
35  
36      private Properties params = new Properties();
37  
38      private int connectionTimeout = Wagon.DEFAULT_CONNECTION_TIMEOUT;
39  
40      private int readTimeout =
41          Integer.parseInt( System.getProperty( "maven.wagon.rto", Integer.toString( Wagon.DEFAULT_READ_TIMEOUT ) ) );
42  
43      private boolean usePreemptive = false;
44  
45      public boolean isUseDefaultHeaders()
46      {
47          return useDefaultHeaders == null || useDefaultHeaders.booleanValue();
48      }
49  
50      public HttpMethodConfiguration setUseDefaultHeaders( boolean useDefaultHeaders )
51      {
52          this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders );
53          return this;
54      }
55  
56      public Boolean getUseDefaultHeaders()
57      {
58          return useDefaultHeaders;
59      }
60  
61      public HttpMethodConfiguration addHeader( String header, String value )
62      {
63          headers.setProperty( header, value );
64          return this;
65      }
66  
67      public Properties getHeaders()
68      {
69          return headers;
70      }
71  
72      public HttpMethodConfiguration setHeaders( Properties headers )
73      {
74          this.headers = headers;
75          return this;
76      }
77  
78      public HttpMethodConfiguration addParam( String param, String value )
79      {
80          params.setProperty( param, value );
81          return this;
82      }
83  
84      public Properties getParams()
85      {
86          return params;
87      }
88  
89      public HttpMethodConfiguration setParams( Properties params )
90      {
91          this.params = params;
92          return this;
93      }
94  
95      public int getConnectionTimeout()
96      {
97          return connectionTimeout;
98      }
99  
100     public HttpMethodConfiguration setConnectionTimeout( int connectionTimeout )
101     {
102         this.connectionTimeout = connectionTimeout;
103         return this;
104     }
105 
106     public int getReadTimeout()
107     {
108         return readTimeout;
109     }
110 
111     public HttpMethodConfiguration setReadTimeout( int readTimeout )
112     {
113         this.readTimeout = readTimeout;
114         return this;
115     }
116 
117     public boolean isUsePreemptive()
118     {
119         return usePreemptive;
120     }
121 
122     public HttpMethodConfiguration setUsePreemptive( boolean usePreemptive )
123     {
124         this.usePreemptive = usePreemptive;
125         return this;
126     }
127 
128     public Header[] asRequestHeaders()
129     {
130         if ( headers == null )
131         {
132             return new Header[0];
133         }
134 
135         Header[] result = new Header[headers.size()];
136 
137         int index = 0;
138         for ( Map.Entry entry : headers.entrySet() )
139         {
140             String key = (String) entry.getKey();
141             String value = (String) entry.getValue();
142 
143             Header header = new BasicHeader( key, value );
144             result[index++] = header;
145         }
146 
147         return result;
148     }
149 
150     HttpMethodConfiguration copy()
151     {
152         HttpMethodConfiguration copy = new HttpMethodConfiguration();
153 
154         copy.setConnectionTimeout( getConnectionTimeout() );
155         copy.setReadTimeout( getReadTimeout() );
156         if ( getHeaders() != null )
157         {
158             copy.setHeaders( getHeaders() );
159         }
160 
161         if ( getParams() != null )
162         {
163             copy.setParams( getParams() );
164         }
165 
166         copy.setUseDefaultHeaders( isUseDefaultHeaders() );
167 
168         return copy;
169     }
170 
171 }