| 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.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.lang.reflect.Method; |
| 24 | import java.net.URI; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.jclouds.javax.annotation.Nullable; |
| 28 | |
| 29 | import org.jclouds.http.HttpRequest; |
| 30 | import org.jclouds.http.HttpRequestFilter; |
| 31 | import org.jclouds.io.Payload; |
| 32 | |
| 33 | import com.google.common.collect.ImmutableList; |
| 34 | import com.google.common.collect.Lists; |
| 35 | import com.google.common.collect.Multimap; |
| 36 | |
| 37 | /** |
| 38 | * Represents a request generated from annotations |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | public class GeneratedHttpRequest<T> extends HttpRequest { |
| 43 | public static <T> Builder<T> builder() { |
| 44 | return new Builder<T>(); |
| 45 | } |
| 46 | |
| 47 | public static class Builder<T> extends HttpRequest.Builder<GeneratedHttpRequest<T>> { |
| 48 | protected Class<T> declaring; |
| 49 | protected Method javaMethod; |
| 50 | protected List<Object> args; |
| 51 | |
| 52 | public Builder<T> declaring(Class<T> declaring) { |
| 53 | this.declaring = checkNotNull(declaring, "declaring"); |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | public Builder<T> javaMethod(Method javaMethod) { |
| 58 | this.javaMethod = checkNotNull(javaMethod, "javaMethod"); |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | public Builder<T> args(Object[] args) { |
| 63 | // TODO make immutable. ImmutableList.of() doesn't accept nulls |
| 64 | return args((args == null) ? ImmutableList.<Object> of() : Lists.newArrayList(args)); |
| 65 | } |
| 66 | |
| 67 | public Builder<T> args(List<Object> args) { |
| 68 | this.args = checkNotNull(args, "args"); |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Builder<T> filters(List<HttpRequestFilter> requestFilters) { |
| 74 | return (Builder<T>) super.filters(requestFilters); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Builder<T> method(String method) { |
| 79 | return (Builder<T>) super.method(method); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Builder<T> endpoint(URI endpoint) { |
| 84 | return (Builder<T>) super.endpoint(endpoint); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Builder<T> skips(char[] skips) { |
| 89 | return (Builder<T>) super.skips(skips); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Builder<T> payload(Payload payload) { |
| 94 | return (Builder<T>) super.payload(payload); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Builder<T> headers(Multimap<String, String> headers) { |
| 99 | return (Builder<T>) super.headers(headers); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public GeneratedHttpRequest<T> build() { |
| 104 | return new GeneratedHttpRequest<T>(method, endpoint, skips, requestFilters, payload, headers, declaring, |
| 105 | javaMethod, args); |
| 106 | } |
| 107 | |
| 108 | public static <Y> Builder<Y> from(HttpRequest input) { |
| 109 | return new Builder<Y>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips()) |
| 110 | .filters(input.getFilters()).payload(input.getPayload()).headers(input.getHeaders()); |
| 111 | } |
| 112 | |
| 113 | public static <Y> Builder<Y> from(GeneratedHttpRequest<Y> input) { |
| 114 | return new Builder<Y>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips()) |
| 115 | .filters(input.getFilters()).payload(input.getPayload()).headers(input.getHeaders()) |
| 116 | .declaring(input.getDeclaring()).javaMethod(input.getJavaMethod()).args(input.getArgs()); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | private final Class<T> declaring; |
| 122 | private final Method javaMethod; |
| 123 | private final List<Object> args; |
| 124 | |
| 125 | GeneratedHttpRequest(String method, URI endpoint, char[] skips, List<HttpRequestFilter> requestFilters, |
| 126 | @Nullable Payload payload, Multimap<String, String> headers, Class<T> declaring, Method javaMethod, |
| 127 | Iterable<Object> args) { |
| 128 | super(method, endpoint, skips, requestFilters, payload, headers); |
| 129 | this.declaring = checkNotNull(declaring, "declaring"); |
| 130 | this.javaMethod = checkNotNull(javaMethod, "javaMethod"); |
| 131 | // TODO make immutable. ImmutableList.of() doesn't accept nulls |
| 132 | this.args = Lists.newArrayList(checkNotNull(args, "args")); |
| 133 | } |
| 134 | |
| 135 | public Class<T> getDeclaring() { |
| 136 | return declaring; |
| 137 | } |
| 138 | |
| 139 | public Method getJavaMethod() { |
| 140 | return javaMethod; |
| 141 | } |
| 142 | |
| 143 | public List<Object> getArgs() { |
| 144 | return args; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Builder<T> toBuilder() { |
| 149 | return Builder.from(this); |
| 150 | } |
| 151 | |
| 152 | } |