001package org.apache.maven.wagon.shared.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.http.Header;
023import org.apache.http.message.BasicHeader;
024import org.apache.maven.wagon.Wagon;
025
026import java.util.Map;
027import java.util.Properties;
028
029/**
030 *
031 */
032public class HttpMethodConfiguration
033{
034
035    private Boolean useDefaultHeaders;
036
037    private Properties headers = new Properties();
038
039    private Properties params = new Properties();
040
041    private int connectionTimeout = Wagon.DEFAULT_CONNECTION_TIMEOUT;
042
043    private int readTimeout =
044        Integer.parseInt( System.getProperty( "maven.wagon.rto", Integer.toString( Wagon.DEFAULT_READ_TIMEOUT ) ) );
045
046    private boolean usePreemptive = false;
047
048    public boolean isUseDefaultHeaders()
049    {
050        return useDefaultHeaders == null || useDefaultHeaders.booleanValue();
051    }
052
053    public HttpMethodConfiguration setUseDefaultHeaders( boolean useDefaultHeaders )
054    {
055        this.useDefaultHeaders = Boolean.valueOf( useDefaultHeaders );
056        return this;
057    }
058
059    public Boolean getUseDefaultHeaders()
060    {
061        return useDefaultHeaders;
062    }
063
064    public HttpMethodConfiguration addHeader( String header, String value )
065    {
066        headers.setProperty( header, value );
067        return this;
068    }
069
070    public Properties getHeaders()
071    {
072        return headers;
073    }
074
075    public HttpMethodConfiguration setHeaders( Properties headers )
076    {
077        this.headers = headers;
078        return this;
079    }
080
081    public HttpMethodConfiguration addParam( String param, String value )
082    {
083        params.setProperty( param, value );
084        return this;
085    }
086
087    public Properties getParams()
088    {
089        return params;
090    }
091
092    public HttpMethodConfiguration setParams( Properties params )
093    {
094        this.params = params;
095        return this;
096    }
097
098    public int getConnectionTimeout()
099    {
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}