blob: 694c298aabf9ae4171c03c64518894a96aa80870 [file] [log] [blame]
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.http;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.http.utils.ModifyRequest;
import org.jclouds.io.Payload;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.rest.binders.BindMapToMatrixParams;
import org.jclouds.rest.binders.BindToJsonPayload;
import org.jclouds.rest.binders.BindToStringPayload;
import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404;
import org.jclouds.util.Strings2;
import com.google.common.base.Function;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Provides;
/**
* Sample test for the behaviour of our Integration Test jetty server.
*
* @author Adrian Cole
*/
public interface IntegrationTestAsyncClient {
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("ROWDY")
public @interface ROWDY {
}
@ROWDY
@Path("/objects/{id}")
ListenableFuture<String> rowdy(@PathParam("id") String path);
@HEAD
@Path("/objects/{id}")
@ExceptionParser(ReturnFalseOnNotFoundOr404.class)
ListenableFuture<Boolean> exists(@PathParam("id") String path);
@GET
@Path("/objects/{id}")
ListenableFuture<String> download(@PathParam("id") String id);
ListenableFuture<HttpResponse> invoke(HttpRequest request);
@GET
@Path("/{path}")
ListenableFuture<String> synch(@PathParam("path") String id);
@GET
@Path("/objects/{id}")
@ExceptionParser(FooOnException.class)
ListenableFuture<String> downloadException(@PathParam("id") String id, HttpRequestOptions options);
static class FooOnException implements Function<Exception, String> {
public String apply(Exception from) {
return "foo";
}
}
@GET
@Path("/objects/{id}")
@ExceptionParser(FooOnException.class)
ListenableFuture<String> synchException(@PathParam("id") String id, @HeaderParam("Range") String header);
@PUT
@Path("/objects/{id}")
ListenableFuture<String> upload(@PathParam("id") String id, @BinderParam(BindToStringPayload.class) String toPut);
@POST
@Path("/objects/{id}")
ListenableFuture<String> post(@PathParam("id") String id, @BinderParam(BindToStringPayload.class) String toPut);
@POST
@Path("/objects/{id}")
ListenableFuture<String> postAsInputStream(@PathParam("id") String id,
@BinderParam(BindToInputStreamPayload.class) String toPut);
static class BindToInputStreamPayload extends BindToStringPayload {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object payload) {
request = super.bindToRequest(request, payload);
request.setPayload(Strings2.toInputStream(payload.toString()));
request.getPayload().getContentMetadata().setContentLength((long) payload.toString().getBytes().length);
return request;
}
}
@Singleton
static class ResponsePayload implements Function<HttpResponse, Multimap<String, String>> {
public Multimap<String, String> apply(HttpResponse from) {
return from.getHeaders();
}
}
@POST
@Path("/objects/{id}")
@ResponseParser(ResponsePayload.class)
ListenableFuture<Multimap<String, String>> postPayloadAndReturnHeaders(@PathParam("id") String id, Payload payload);
@POST
@Path("/objects/{id}")
@MapBinder(BindToJsonPayload.class)
ListenableFuture<String> postJson(@PathParam("id") String id, @PayloadParam("key") String toPut);
@POST
@Path("/objects/{id}/action/{action}")
ListenableFuture<String> action(@PathParam("id") String id, @PathParam("action") String action,
@BinderParam(BindMapToMatrixParams.class) Map<String, String> options);
@GET
@Path("/objects/{id}")
@RequestFilters(Filter.class)
ListenableFuture<String> downloadFilter(@PathParam("id") String id, @HeaderParam("filterme") String header);
static class Filter implements HttpRequestFilter {
public HttpRequest filter(HttpRequest request) throws HttpException {
if (request.getHeaders().containsKey("filterme")) {
request = ModifyRequest.replaceHeader(request, "test", "test");
}
return request;
}
}
@GET
@Path("/objects/{id}")
ListenableFuture<String> download(@PathParam("id") String id, @HeaderParam("test") String header);
@GET
@Path("/objects/{id}")
@XMLResponseParser(BarHandler.class)
ListenableFuture<String> downloadAndParse(@PathParam("id") String id);
public static class BarHandler extends ParseSax.HandlerWithResult<String> {
private String bar = null;
private StringBuilder currentText = new StringBuilder();
@Override
public void endElement(String uri, String name, String qName) {
if (qName.equals("bar")) {
bar = currentText.toString();
}
currentText = new StringBuilder();
}
@Override
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
@Override
public String getResult() {
return bar;
}
}
@Provides
StringBuffer newStringBuffer();
}