public static String getBucketName(HttpRequest req) { checkArgument(req instanceof GeneratedHttpRequest, "this should be a generated http request"); GeneratedHttpRequest request = GeneratedHttpRequest.class.cast(req); String bucketName = null; for (int i = 0; i < request.getInvocation().getInvokable().getParameters().size(); i++) { if (any(Arrays.asList(request.getInvocation().getInvokable().getParameters().get(i).getAnnotations()), ANNOTATIONTYPE_BUCKET)) { bucketName = (String) request.getInvocation().getArgs().get(i); break; } } return bucketName; }
/** * invocation without arguments. * * @throws IllegalArgumentException * if in invokable requires arguments */ public static Invocation create(Invokable<?, ?> invokable) { checkArgument( invokable.getParameters().size() == 0 || (invokable.getParameters().size() == 1 && invokable.isVarArgs()), "please specify arguments to %s", invokable); return create(invokable, ImmutableList.of()); }
/** * @param args * as these represent parameters, can contain nulls */ public static Invocation create(Invokable<?, ?> invokable, List<Object> args) { return new Invocation(invokable, args); }
public Invocation apply(Invocation in) { return Invocation.create( checkNotNull(cache.getIfPresent(in.getInvokable()), "invokable %s not in %s", in.getInvokable(), cache), in.getArgs()); } };
@Override public String toString() { String callerString = caller != null ? String.format("%s.%s%s", caller.getInvokable().getOwnerType().getRawType().getSimpleName(), caller.getInvokable().getName(), caller.getArgs()) : null; return Objects.toStringHelper("").omitNullValues().add("caller", callerString).toString(); } }
@Test public void testHostPrefix() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestVirtualHost.class, "getPrefix", String.class, String.class); GeneratedHttpRequest request = processor.apply(Invocation.create(method, ImmutableList.<Object> of("1", "holy"))); assertEquals(request.getEndpoint().getHost(), "holy.localhost"); assertEquals(request.getEndpoint().getPath(), "/1"); assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getHeaders().size(), 0); }
@Test public void testBuildOneHeader() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestHeader.class, "oneHeader", String.class); Multimap<String, String> headers = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/robot")); }
@Test public void testBuildTwoQuery() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestQueryReplace.class, "twoQuery", String.class); String query = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))) .getEndpoint().getQuery(); assertEquals(query, "slash=/robot&hyphen=-robot"); }
@Test public void testBuildOneForm() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestFormReplace.class, "oneForm", String.class); Object form = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))) .getPayload().getRawContent(); assertEquals(form, "x-amz-copy-source=/robot"); }
@Test public void testCorrect() throws SecurityException, NoSuchMethodException { Invokable<?, Object> testPayload = method(TestPayload.class, "testPayload", String.class); GeneratedHttpRequest request = GeneratedHttpRequest.builder() .invocation(Invocation.create(testPayload, ImmutableList.<Object> of("robot"))) .method("POST").endpoint("http://localhost").build(); GeneratedHttpRequest newRequest = binder().bindToRequest(request, ImmutableMap.<String, Object> of("fooble", "robot")); assertEquals(newRequest.getRequestLine(), request.getRequestLine()); assertEquals(newRequest.getPayload().getRawContent(), "name robot"); }
@Test(expectedExceptions = ClassCastException.class) public void testWrongPredicateTypeLiteral() throws Exception { Invocation invocation = Invocation.create(method(WrongValidator.class, "method", Integer.class), ImmutableList.<Object> of(55)); new InputParamValidator(injector).validateMethodParametersOrThrow(invocation, invocation.getInvokable().getParameters()); }
public void testRequestFilterOverrideOnRequest() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestRequestFilter.class, "getOverride", HttpRequest.class); GeneratedHttpRequest request = processor.apply( Invocation.create(method, ImmutableList.<Object> of(HttpRequest.builder().method("GET").endpoint("http://localhost") .addHeader("foo", "bar").build()))); assertEquals(request.getFilters().size(), 1); assertEquals(request.getHeaders().size(), 1); assertEquals(request.getFilters().get(0).getClass(), TestRequestFilter2.class); }
public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException { GeneratedHttpRequest request = GeneratedHttpRequest.builder() .method("GET") .endpoint("http://localhost") .invocation( Invocation.create(method(TestTransformers.class, "oneTransformerWithContext"), ImmutableList.of())).build(); Function<HttpResponse, ?> transformer = this.transformer.apply(request); assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(((ReturnStringIf200Context) transformer).request, request); }
public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertTrue(e.getArgs() instanceof ImmutableList); assertEquals(e.getArgs().get(0), "foo"); e.getArgs().add("bar"); throw new AssertionError("shouldn't be able to mutate the list!"); } };
public Object apply(Invocation e) { assertNotNull(e.getArgs()); assertNull(e.getArgs().get(0)); e.getArgs().add("foo"); throw new AssertionError("shouldn't be able to mutate the list!"); } };
private boolean checkPresentOrNullable(Invocation invocation, String paramKey, int argIndex, Object arg) { if (arg == null && !invocation.getInvokable().getParameters().get(argIndex).isAnnotationPresent(Nullable.class)) throw new NullPointerException(format("param{%s} for invocation %s.%s", paramKey, invocation.getInvokable() .getOwnerType().getRawType().getSimpleName(), invocation.getInvokable().getName())); return true; }
protected VApp findVAppInArgsOrNull(GeneratedHttpRequest gRequest) { for (Object arg : gRequest.getInvocation().getArgs()) { if (arg instanceof VApp) { return (VApp) arg; } else if (arg instanceof VApp[]) { VApp[] vapps = (VApp[]) arg; return (vapps.length > 0) ? vapps[0] : null; } } return null; }
private void addHeaderIfAnnotationPresentOnMethod(Multimap<String, String> headers, Invocation invocation, Multimap<String, ?> tokenValues) { if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Headers.class)) { Headers header = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } if (invocation.getInvokable().isAnnotationPresent(Headers.class)) { Headers header = invocation.getInvokable().getAnnotation(Headers.class); addHeader(headers, header, tokenValues); } }
public static String getBucketName(HttpRequest req) { checkArgument(req instanceof GeneratedHttpRequest, "this should be a generated http request"); GeneratedHttpRequest request = GeneratedHttpRequest.class.cast(req); String bucketName = null; List<Parameter> parameters = Reflection2.getInvokableParameters(request.getInvocation().getInvokable()); for (int i = 0; i < parameters.size(); i++) { if (any(Arrays.asList(parameters.get(i).getAnnotations()), ANNOTATIONTYPE_BUCKET)) { bucketName = (String) request.getInvocation().getArgs().get(i); break; } } return bucketName; }
public ContainerProperties apply(HttpResponse from) { MutableContainerPropertiesWithMetadata to = new MutableContainerPropertiesWithMetadataImpl(); to.setName(request.getInvocation().getArgs().get(0).toString()); addUserMetadataTo(from, to); parseLastModifiedOrThrowException(from, to); addETagTo(from, to); to.setUrl(request.getEndpoint()); return to; }