View Javadoc

1   /*
2    * Copyright 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  
17  package org.apache.commons.jelly.tags.http;
18  
19  import java.net.MalformedURLException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.httpclient.HttpMethod;
24  import org.apache.commons.httpclient.NameValuePair;
25  import org.apache.commons.httpclient.methods.MultipartPostMethod;
26  import org.apache.commons.httpclient.methods.multipart.Part;
27  import org.apache.commons.httpclient.methods.multipart.StringPart;
28  
29  /***
30   * A Multipart MIME message post
31   *
32   * This tag should contain one or more <part> tags, to
33   * specify the multiple parts of the message
34   *
35   * Example:
36   * <pre>
37   *   &lt;mppost uri="http://localhost?doit"&gt;
38   *       &lt;part name="user" type="text/plain"&gt;Fred&lt;/part&gt;
39   *       &lt;part name="data"  type="text/plain"&gt;This is the second part of the message&lt;/part&gt;
40   *   &lt;/mppost&gt;
41   *</pre>
42   *
43   * @author <a href="mailto:wkeese@yahoo.com">Bill Keese</a>
44   *
45   * @since ???
46   */
47  public class MultipartPostTag extends PostTag {
48  
49      /*** the post method */
50      private MultipartPostMethod _postMethod;
51  
52      /*** list of parts as name value pairs */
53      private List _parts;
54  
55      /*** Creates a new instance of MppostTag */
56      public MultipartPostTag() {
57        _parts = new ArrayList();
58      }
59  
60      /***
61       * Return a {@link HttpUrlMethod method} to be used for multi-part post'ing
62       *
63       * @return a HttpUrlMethod implementation
64       * @throws MalformedURLException when the {@link getUrl() url} or
65       * {@link #getPath() path} is invalid
66       */
67      protected HttpMethod getHttpMethod() throws MalformedURLException {
68          if (_postMethod == null) {
69              _postMethod = new MultipartPostMethod(getResolvedUrl());
70          }
71          return _postMethod;
72      }
73  
74      /***
75       * Add a part to the message
76       *
77       * @param name the parameter name
78       * @param value the parameter value
79       */
80      public void addPart(Part p) {
81          _parts.add(p);
82      }
83  
84      /***
85       * Set the current parameters on the url method ready for processing
86       *
87       * This method basically
88       * It <strong>must</strong> be called after
89       *  {@link getHttpUrlMethod}
90       */
91      protected void setParameters(HttpMethod method) {
92          for (int index = 0; index < _parts.size(); index++) {
93              ((MultipartPostMethod) method).addPart( (Part) _parts.get(index) );
94          }
95      }
96  }