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.rest.binders; |
20 | |
21 | import java.net.URI; |
22 | import java.util.Map; |
23 | |
24 | import javax.inject.Inject; |
25 | import javax.inject.Provider; |
26 | import javax.inject.Singleton; |
27 | import javax.ws.rs.core.UriBuilder; |
28 | |
29 | import org.jclouds.http.HttpRequest; |
30 | import org.jclouds.io.Payloads; |
31 | import org.jclouds.rest.MapBinder; |
32 | import org.jclouds.rest.annotations.Payload; |
33 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
34 | |
35 | /** |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | @Singleton |
40 | public class BindMapToStringPayload implements MapBinder { |
41 | protected final Provider<UriBuilder> uriBuilders; |
42 | |
43 | @Inject |
44 | public BindMapToStringPayload(Provider<UriBuilder> uriBuilders) { |
45 | this.uriBuilders = uriBuilders; |
46 | } |
47 | |
48 | @SuppressWarnings("unchecked") |
49 | @Override |
50 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
51 | GeneratedHttpRequest<?> r = GeneratedHttpRequest.class.cast(request); |
52 | String payload = r.getJavaMethod().getAnnotation(Payload.class).value(); |
53 | if (postParams.size() > 0) { |
54 | UriBuilder builder = uriBuilders.get(); |
55 | builder.uri(URI.create("http://test/")); |
56 | builder.path(payload); |
57 | URI fake = builder.buildFromMap(postParams); |
58 | payload = fake.getPath().substring(1); |
59 | } |
60 | return (R) request.toBuilder().payload(Payloads.newStringPayload(payload)).build(); |
61 | } |
62 | |
63 | @Override |
64 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
65 | throw new IllegalArgumentException("this is a map binder"); |
66 | } |
67 | |
68 | } |