Skip to content

Commit

Permalink
Put MP Rest Client invokedMethod in ClientRequestContext properties
Browse files Browse the repository at this point in the history
This partially resolves issue
[121](eclipse/microprofile-rest-client#121)
for MicroProfile Rest Client 1.2 - enabling `ClientRequestFilter`s to
access the Rest Client interface method being invoked.
  • Loading branch information
andymc12 committed Oct 3, 2018
1 parent 4da4203 commit 1542c81
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
Expand Up @@ -725,6 +725,18 @@ protected void handleCookies(Method m,
});
});
}

protected Message createMessage(Object body,
OperationResourceInfo ori,
MultivaluedMap<String, String> headers,
URI currentURI,
Exchange exchange,
Map<String, Object> invocationContext,
boolean isProxy) {
return createMessage(body, ori.getHttpMethod(), headers, currentURI,
exchange, invocationContext, isProxy);
}

//CHECKSTYLE:OFF
protected Object doChainedInvocation(URI uri,
MultivaluedMap<String, String> headers,
Expand All @@ -743,8 +755,7 @@ protected Object doChainedInvocation(URI uri,
if (loader != null) {
origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
}
Message outMessage = createMessage(body, ori.getHttpMethod(), headers, uri,
exchange, invocationContext, true);
Message outMessage = createMessage(body, ori, headers, uri, exchange, invocationContext, true);
if (bodyIndex != -1) {
outMessage.put(Type.class, ori.getMethodToInvoke().getGenericParameterTypes()[bodyIndex]);
}
Expand Down
Expand Up @@ -21,12 +21,14 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutorService;

import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.ClientProxyImpl;
Expand All @@ -36,6 +38,7 @@
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.microprofile.client.MPRestClientCallback;
import org.apache.cxf.microprofile.client.MicroProfileClientProviderFactory;
Expand Down Expand Up @@ -139,4 +142,20 @@ protected Class<?> getReturnType(Method method, Message outMessage) {
}
return returnType;
}

@Override
protected Message createMessage(Object body,
OperationResourceInfo ori,
MultivaluedMap<String, String> headers,
URI currentURI,
Exchange exchange,
Map<String, Object> invocationContext,
boolean proxy) {
Method m = ori.getMethodToInvoke();
Map<String, Object> filterProps = new HashMap<>();
filterProps.put("org.eclipse.microprofile.rest.client.invokedMethod", m);
Message msg = super.createMessage(body, ori, headers, currentURI, exchange, invocationContext, proxy);
msg.getExchange().put("jaxrs.filter.properties", filterProps);
return msg;
}
}
Expand Up @@ -18,15 +18,18 @@
*/
package org.apache.cxf.microprofile.client;

import java.net.URI;
import java.net.URL;

import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.apache.cxf.microprofile.client.mock.EchoClientReqFilter;
import org.apache.cxf.microprofile.client.mock.ExceptionMappingClient;
import org.apache.cxf.microprofile.client.mock.HighPriorityClientReqFilter;
import org.apache.cxf.microprofile.client.mock.HighPriorityMBW;
import org.apache.cxf.microprofile.client.mock.InvokedMethodClientRequestFilter;
import org.apache.cxf.microprofile.client.mock.LowPriorityClientReqFilter;
import org.apache.cxf.microprofile.client.mock.MyClient;
import org.apache.cxf.microprofile.client.mock.NoSuchEntityException;
Expand Down Expand Up @@ -131,6 +134,23 @@ public void testDefaultResponseExceptionMapper() throws Exception {
fail(r, "Did not throw expected mapped exception: WebApplicationException");
}

@Test
public void testClientRequestFilterCanAccessInvokedMethod() throws Exception {
InterfaceWithoutProvidersDefined client = RestClientBuilder.newBuilder()
.register(InvokedMethodClientRequestFilter.class)
.baseUri(new URI("http://localhost:8080/neverUsed"))
.build(InterfaceWithoutProvidersDefined.class);

Response response = client.executePut("foo", "bar");
assertEquals(200, response.getStatus());
assertEquals(Response.class.getName(), response.getHeaderString("ReturnType"));
assertEquals("PUT", response.getHeaderString("PUT"));
assertEquals("/{id}", response.getHeaderString("Path"));
assertEquals(String.class.getName(), response.getHeaderString("Parm1"));
assertEquals(PathParam.class.getName(), response.getHeaderString("Parm1Annotation"));
assertEquals(String.class.getName(), response.getHeaderString("Parm2"));
}

private void fail(Response r, String failureMessage) {
System.out.println(r.getStatus());
fail(failureMessage);
Expand Down
@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.apache.cxf.microprofile.client.mock;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.Response;

public class InvokedMethodClientRequestFilter implements ClientRequestFilter {

@Override
public void filter(ClientRequestContext ctx) throws IOException {
try {
Method m = (Method) ctx.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

Path path = m.getAnnotation(Path.class);
ctx.abortWith(Response.ok("OK")
.header("ReturnType", m.getReturnType().getName())
.header("PUT", m.getAnnotation(PUT.class) == null ? "null" : "PUT")
.header("Path", path == null ? "null" : path.value())
.header("Parm1", m.getParameters()[0].getType().getName())
.header("Parm1Annotation",
m.getParameters()[0].getAnnotations()[0].annotationType().getName())
.header("Parm2", m.getParameters()[1].getType().getName())
.build());
} catch (Throwable t) {
t.printStackTrace();
ctx.abortWith(Response.serverError().build());
}
}

}

0 comments on commit 1542c81

Please sign in to comment.