1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 * <mppost uri="http://localhost?doit">
38 * <part name="user" type="text/plain">Fred</part>
39 * <part name="data" type="text/plain">This is the second part of the message</part>
40 * </mppost>
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 }