1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.io.payloads; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static com.google.common.collect.Multimaps.forMap; |
23 | |
24 | import java.util.LinkedHashMap; |
25 | |
26 | import org.jclouds.javax.annotation.Nullable; |
27 | import javax.ws.rs.core.HttpHeaders; |
28 | |
29 | import org.jclouds.io.Payload; |
30 | import org.jclouds.io.Payloads; |
31 | |
32 | import com.google.common.collect.ImmutableMultimap; |
33 | import com.google.common.collect.Multimap; |
34 | |
35 | /** |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | public class Part extends DelegatingPayload { |
40 | final String name; |
41 | final Multimap<String, String> headers; |
42 | |
43 | private static class PartMap extends LinkedHashMap<String, String> { |
44 | |
45 | /** The serialVersionUID */ |
46 | private static final long serialVersionUID = -287387556008320212L; |
47 | |
48 | static Part.PartMap create(String name) { |
49 | Part.PartMap map = new PartMap(); |
50 | map.put("Content-Disposition", String.format("form-data; name=\"%s\"", checkNotNull(name, |
51 | "name"))); |
52 | return map; |
53 | } |
54 | |
55 | static Part.PartMap create(String name, String filename) { |
56 | Part.PartMap map = new PartMap(); |
57 | map.put("Content-Disposition", String.format("form-data; name=\"%s\"; filename=\"%s\"", |
58 | checkNotNull(name, "name"), checkNotNull(filename, "filename"))); |
59 | return map; |
60 | } |
61 | |
62 | Part.PartMap contentType(@Nullable String type) { |
63 | if (type != null) |
64 | put(HttpHeaders.CONTENT_TYPE, checkNotNull(type, "type")); |
65 | return this; |
66 | } |
67 | |
68 | public static Part.PartMap create(String name, Payload delegate, Part.PartOptions options) { |
69 | String filename = options != null ? options.getFilename() : null; |
70 | if (delegate instanceof FilePayload) |
71 | filename = FilePayload.class.cast(delegate).getRawContent().getName(); |
72 | Part.PartMap returnVal; |
73 | returnVal = (filename != null) ? create(name, filename) : create(name); |
74 | if (options != null) |
75 | returnVal.contentType(options.getContentType()); |
76 | return returnVal; |
77 | |
78 | } |
79 | } |
80 | |
81 | private Part(String name, Part.PartMap map, Payload delegate) { |
82 | super(delegate); |
83 | this.name = name; |
84 | this.headers = ImmutableMultimap.copyOf(forMap((checkNotNull(map, "headers")))); |
85 | } |
86 | |
87 | public static class PartOptions { |
88 | private String contentType; |
89 | private String filename; |
90 | |
91 | public Part.PartOptions contentType(String contentType) { |
92 | this.contentType = checkNotNull(contentType, "contentType"); |
93 | return this; |
94 | } |
95 | |
96 | public Part.PartOptions filename(String filename) { |
97 | this.filename = checkNotNull(filename, "filename"); |
98 | return this; |
99 | } |
100 | |
101 | public static class Builder { |
102 | public static Part.PartOptions contentType(String contentType) { |
103 | return new PartOptions().contentType(contentType); |
104 | } |
105 | |
106 | public static Part.PartOptions filename(String filename) { |
107 | return new PartOptions().filename(filename); |
108 | } |
109 | } |
110 | |
111 | public String getContentType() { |
112 | return contentType; |
113 | } |
114 | |
115 | public String getFilename() { |
116 | return filename; |
117 | } |
118 | } |
119 | |
120 | public static Part create(String name, String value) { |
121 | return new Part(name, PartMap.create(name), Payloads.newStringPayload(value)); |
122 | } |
123 | |
124 | public static Part create(String name, Payload delegate, Part.PartOptions options) { |
125 | return new Part(name, PartMap.create(name, delegate, options), delegate); |
126 | } |
127 | |
128 | public Multimap<String, String> getHeaders() { |
129 | return headers; |
130 | } |
131 | |
132 | public String getName() { |
133 | return name; |
134 | } |
135 | } |