use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ClientProxyImpl method getBodyIndex.
private static int getBodyIndex(MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori) {
List<Parameter> list = map.get(ParameterType.REQUEST_BODY);
int index = list == null || list.size() > 1 ? -1 : list.get(0).getIndex();
if (ori.isSubResourceLocator() && index != -1) {
reportInvalidResourceMethod(ori.getMethodToInvoke(), "NO_BODY_IN_SUBRESOURCE");
}
return index;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ClientProxyImpl method handleForm.
private MultivaluedMap<String, String> handleForm(Method m, Object[] params, MultivaluedMap<ParameterType, Parameter> map, List<Parameter> beanParams) {
MultivaluedMap<String, String> form = new MetadataMap<String, String>();
List<Parameter> fm = getParameters(map, ParameterType.FORM);
for (Parameter p : fm) {
addFormValue(form, p.getName(), params[p.getIndex()], getParamAnnotations(m, p));
}
for (Parameter p : beanParams) {
Map<String, BeanPair> values = getValuesFromBeanParam(params[p.getIndex()], FormParam.class);
for (Map.Entry<String, BeanPair> entry : values.entrySet()) {
addFormValue(form, entry.getKey(), entry.getValue().getValue(), entry.getValue().getAnns());
}
}
return form;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ClientProxyImpl method getParametersInfo.
private static MultivaluedMap<ParameterType, Parameter> getParametersInfo(Method m, Object[] params, OperationResourceInfo ori) {
MultivaluedMap<ParameterType, Parameter> map = new MetadataMap<ParameterType, Parameter>();
List<Parameter> parameters = ori.getParameters();
if (parameters.isEmpty()) {
return map;
}
int requestBodyParam = 0;
int multipartParam = 0;
for (Parameter p : parameters) {
if (isIgnorableParameter(m, p)) {
continue;
}
if (p.getType() == ParameterType.REQUEST_BODY) {
requestBodyParam++;
if (getMultipart(ori, p.getIndex()) != null) {
multipartParam++;
}
}
map.add(p.getType(), p);
}
if (map.containsKey(ParameterType.REQUEST_BODY)) {
if (requestBodyParam > 1 && requestBodyParam != multipartParam) {
reportInvalidResourceMethod(ori.getMethodToInvoke(), "SINGLE_BODY_ONLY");
}
if (map.containsKey(ParameterType.FORM)) {
reportInvalidResourceMethod(ori.getMethodToInvoke(), "ONLY_FORM_ALLOWED");
}
}
return map;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ClientProxyImpl method invoke.
/**
* Updates the current state if Client method is invoked, otherwise
* does the remote invocation or returns a new proxy if subresource
* method is invoked. Can throw an expected exception if ResponseExceptionMapper
* is registered
*/
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
Class<?> declaringClass = m.getDeclaringClass();
if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass || Object.class == declaringClass) {
return m.invoke(this, params);
}
resetResponse();
OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
if (ori == null) {
reportInvalidResourceMethod(m, "INVALID_RESOURCE_METHOD");
}
MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(m, params, ori);
List<Parameter> beanParamsList = getParameters(types, ParameterType.BEAN);
int bodyIndex = getBodyIndex(types, ori);
List<Object> pathParams = getPathParamValues(m, params, types, beanParamsList, ori, bodyIndex);
UriBuilder builder = getCurrentBuilder().clone();
if (isRoot) {
addNonEmptyPath(builder, ori.getClassResourceInfo().getURITemplate().getValue());
}
addNonEmptyPath(builder, ori.getURITemplate().getValue());
handleMatrixes(m, params, types, beanParamsList, builder);
handleQueries(m, params, types, beanParamsList, builder);
URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();
MultivaluedMap<String, String> headers = getHeaders();
MultivaluedMap<String, String> paramHeaders = new MetadataMap<String, String>();
handleHeaders(m, params, paramHeaders, beanParamsList, types);
handleCookies(m, params, paramHeaders, beanParamsList, types);
if (ori.isSubResourceLocator()) {
ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
if (subCri == null) {
reportInvalidResourceMethod(m, "INVALID_SUBRESOURCE");
}
MultivaluedMap<String, String> subHeaders = paramHeaders;
if (inheritHeaders) {
subHeaders.putAll(headers);
}
ClientState newState = getState().newState(uri, subHeaders, getTemplateParametersMap(ori.getURITemplate(), pathParams));
ClientProxyImpl proxyImpl = new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
proxyImpl.setConfiguration(getConfiguration());
return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
}
headers.putAll(paramHeaders);
getState().setTemplates(getTemplateParametersMap(ori.getURITemplate(), pathParams));
Object body = null;
if (bodyIndex != -1) {
body = params[bodyIndex];
if (body == null) {
bodyIndex = -1;
}
} else if (types.containsKey(ParameterType.FORM)) {
body = handleForm(m, params, types, beanParamsList);
} else if (types.containsKey(ParameterType.REQUEST_BODY)) {
body = handleMultipart(types, ori, params);
}
setRequestHeaders(headers, ori, types.containsKey(ParameterType.FORM), body == null ? null : body.getClass(), m.getReturnType());
try {
return doChainedInvocation(uri, headers, ori, params, body, bodyIndex, null, null);
} finally {
resetResponseStateImmediatelyIfNeeded();
}
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class SwaggerParseUtilsTest method testConvertSimpleDocToUserApp.
@Test
public void testConvertSimpleDocToUserApp() {
UserApplication ap = SwaggerParseUtils.getUserApplication("/swagger20.json");
assertNotNull(ap);
assertEquals("/services/helloservice", ap.getBasePath());
Map<String, UserResource> map = ap.getResourcesAsMap();
assertEquals(2, map.size());
UserResource ur = map.get("sayHello");
assertNotNull(ur);
assertEquals("/", ur.getPath());
assertEquals(1, ur.getOperations().size());
UserOperation op = ur.getOperations().get(0);
assertEquals("sayHello", op.getName());
assertEquals("/sayHello/{a}", op.getPath());
assertEquals("GET", op.getVerb());
assertEquals("text/plain", op.getProduces());
assertEquals(1, op.getParameters().size());
Parameter param1 = op.getParameters().get(0);
assertEquals("a", param1.getName());
assertEquals(ParameterType.PATH, param1.getType());
assertEquals(String.class, param1.getJavaType());
UserResource ur2 = map.get("sayHello2");
assertNotNull(ur2);
assertEquals("/", ur2.getPath());
assertEquals(1, ur2.getOperations().size());
UserOperation op2 = ur2.getOperations().get(0);
assertEquals("sayHello", op2.getName());
assertEquals("/sayHello2/{a}", op2.getPath());
assertEquals("GET", op2.getVerb());
assertEquals("text/plain", op2.getProduces());
assertEquals(1, op2.getParameters().size());
Parameter param2 = op.getParameters().get(0);
assertEquals("a", param2.getName());
assertEquals(ParameterType.PATH, param2.getType());
assertEquals(String.class, param2.getJavaType());
}
Aggregations