KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > latka > http > MethodFactory


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.latka.http;
17
18 import java.net.URL JavaDoc;
19
20 import org.apache.commons.httpclient.HttpMethod;
21 import org.apache.commons.httpclient.methods.GetMethod;
22 import org.apache.commons.httpclient.methods.PostMethod;
23 import org.apache.commons.httpclient.methods.HeadMethod;
24
25 /**
26  * This factory provides a single point to obtain
27  * implementations of HttpMethod base.
28  *
29  * @version $Revision $
30  */

31 public final class MethodFactory {
32
33     /**
34      * Private default constructor
35      */

36     private MethodFactory() {
37     }
38
39     /**
40      * Creates and returns an object implementing the
41      * HttpMethod interface.
42      *
43      * @param method - method type to create
44      * @return HttpMethod instance
45      */

46     public static HttpMethod getInstance(int method, URL JavaDoc url) {
47         HttpMethod httpMethod = null;
48         switch (method) {
49             case Request.HTTP_METHOD_GET:
50                 httpMethod = new GetMethod(url.getPath());
51                 break;
52             case Request.HTTP_METHOD_POST:
53                 httpMethod = new PostMethod(url.getPath());
54                 break;
55             case Request.HTTP_METHOD_HEAD:
56                 httpMethod = new HeadMethod(url.getPath());
57                 break;
58             default:
59                 throw new IllegalArgumentException JavaDoc("Unsupported HTTP Method");
60         }
61         return httpMethod;
62     }
63 }
64
65
Popular Tags