001    /*
002     * Copyright 1999-2002,2004 The Apache Software Foundation.
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     *      http://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    package org.apache.commons.latka.http;
017    
018    import java.net.URL;
019    
020    import org.apache.commons.httpclient.HttpMethod;
021    import org.apache.commons.httpclient.methods.GetMethod;
022    import org.apache.commons.httpclient.methods.PostMethod;
023    import org.apache.commons.httpclient.methods.HeadMethod;
024    
025    /**
026     * This factory provides a single point to obtain
027     * implementations of HttpMethod base.
028     *
029     * @version $Revision $
030     */
031    public final class MethodFactory {
032    
033        /**
034         * Private default constructor
035         */
036        private MethodFactory() {
037        }
038    
039        /**
040         * Creates and returns an object implementing the
041         * HttpMethod interface.
042         *
043         * @param method - method type to create
044         * @return HttpMethod instance
045         */
046        public static HttpMethod getInstance(int method, URL url) {
047            HttpMethod httpMethod = null;
048            switch (method) {
049                case Request.HTTP_METHOD_GET:
050                    httpMethod = new GetMethod(url.getPath());
051                    break;
052                case Request.HTTP_METHOD_POST:
053                    httpMethod = new PostMethod(url.getPath());
054                    break;
055                case Request.HTTP_METHOD_HEAD:
056                    httpMethod = new HeadMethod(url.getPath());
057                    break;
058                default:
059                    throw new IllegalArgumentException("Unsupported HTTP Method");
060            }
061            return httpMethod;
062        }
063    }
064