use of javax.ws.rs.core.PathSegment in project cxf by apache.
the class SourceGenerator method writeResourceMethod.
// CHECKSTYLE:OFF
private void writeResourceMethod(Element methodEl, String classPackage, Set<String> imports, StringBuilder sbCode, ContextInfo info, boolean isRoot, String currentPath, Map<String, Integer> methodNameMap) {
// CHECKSTYLE:ON
StringBuilder sbMethodCode = sbCode;
StringBuilder sbMethodDocs = null;
StringBuilder sbMethodRespDocs = null;
boolean doCreateJavaDocs = isJavaDocNeeded(info);
if (doCreateJavaDocs) {
sbMethodCode = new StringBuilder();
sbMethodDocs = startMethodDocs(methodEl);
sbMethodRespDocs = new StringBuilder();
}
boolean isResourceElement = "resource".equals(methodEl.getLocalName());
Element resourceEl = isResourceElement ? methodEl : (Element) methodEl.getParentNode();
List<Element> responseEls = getWadlElements(methodEl, "response");
List<Element> requestEls = getWadlElements(methodEl, "request");
Element firstRequestEl = requestEls.size() >= 1 ? requestEls.get(0) : null;
List<Element> allRequestReps = getWadlElements(firstRequestEl, "representation");
List<Element> requestRepsWithElements = new LinkedList<Element>();
boolean duplicatesAvailable = getRepsWithElements(allRequestReps, requestRepsWithElements, info.getGrammarInfo());
String methodName = methodEl.getAttribute("name");
final String methodNameLowerCase = methodName.toLowerCase();
String idAttribute = methodEl.getAttribute("id");
final String id = idAttribute.isEmpty() ? methodNameLowerCase : idAttribute;
final boolean responseRequired = isMethodMatched(responseMethods, methodNameLowerCase, id);
final boolean suspendedAsync = responseRequired ? false : isMethodMatched(suspendedAsyncMethods, methodNameLowerCase, id);
final boolean oneway = isMethodMatched(onewayMethods, methodNameLowerCase, id);
boolean jaxpSourceRequired = requestRepsWithElements.size() > 1 && !supportMultipleRepsWithElements;
int numOfMethods = jaxpSourceRequired ? 1 : requestRepsWithElements.size();
for (int i = 0; i < numOfMethods; i++) {
List<Element> requestReps = allRequestReps;
Element requestRepWithElement = requestRepsWithElements.get(i);
String suffixName = "";
if (supportMultipleRepsWithElements && requestRepWithElement != null && requestRepsWithElements.size() > 1) {
String elementRef = requestRepWithElement.getAttribute("element");
int index = elementRef.indexOf(":");
suffixName = elementRef.substring(index + 1).replace("-", "");
if (duplicatesAvailable) {
String mediaType = requestRepWithElement.getAttribute("mediaType");
if (!StringUtils.isEmpty(mediaType)) {
String subType = MediaType.valueOf(mediaType).getSubtype();
String[] parts = StringUtils.split(subType, "\\+");
if (parts.length == 2) {
suffixName += StringUtils.capitalize(parts[1]);
} else {
suffixName += StringUtils.capitalize(parts[0].replaceAll("[\\.-]", ""));
}
}
}
requestReps = Collections.singletonList(requestRepWithElement);
}
if (writeAnnotations(info.isInterfaceGenerated())) {
sbMethodCode.append(TAB);
if (methodNameLowerCase.length() > 0) {
if (HTTP_METHOD_ANNOTATIONS.containsKey(methodNameLowerCase)) {
writeAnnotation(sbMethodCode, imports, HTTP_METHOD_ANNOTATIONS.get(methodNameLowerCase), null, true, true);
} else {
writeCustomHttpMethod(info, classPackage, methodName, sbMethodCode, imports);
}
writeFormatAnnotations(requestReps, sbMethodCode, imports, true, null);
List<Element> responseReps = getWadlElements(getOKResponse(responseEls), "representation");
writeFormatAnnotations(responseReps, sbMethodCode, imports, false, requestRepWithElement);
if (supportBeanValidation && !responseRequired && isRepWithElementAvailable(responseReps, info.getGrammarInfo())) {
addImport(imports, BEAN_VALID_FULL_NAME);
sbMethodCode.append("@").append(BEAN_VALID_SIMPLE_NAME).append(getLineSep()).append(TAB);
}
if (oneway) {
addImport(imports, Oneway.class.getName());
sbMethodCode.append("@").append(Oneway.class.getSimpleName()).append(getLineSep()).append(TAB);
}
}
if (!isRoot && !"/".equals(currentPath)) {
writeAnnotation(sbMethodCode, imports, Path.class, currentPath, true, true);
}
} else {
sbMethodCode.append(getLineSep()).append(TAB);
}
if (!info.isInterfaceGenerated()) {
sbMethodCode.append("public ");
}
boolean responseTypeAvailable = true;
if (methodNameLowerCase.length() > 0) {
responseTypeAvailable = writeResponseType(responseEls, requestRepWithElement, sbMethodCode, sbMethodRespDocs, imports, info, responseRequired, suspendedAsync);
String genMethodName = id + suffixName;
if (methodNameLowerCase.equals(genMethodName) && idAttribute.isEmpty()) {
List<PathSegment> segments = JAXRSUtils.getPathSegments(currentPath, true, true);
StringBuilder sb = new StringBuilder();
for (PathSegment ps : segments) {
String pathSeg = ps.getPath().replaceAll("\\{", "").replaceAll("\\}", "");
int index = pathSeg.indexOf(":");
if (index > 0) {
pathSeg = pathSeg.substring(0, index);
}
sb.append(pathSeg);
}
genMethodName += firstCharToUpperCase(sb.toString());
}
genMethodName = genMethodName.replace("-", "");
Integer value = methodNameMap.get(genMethodName);
if (value == null) {
value = 0;
}
methodNameMap.put(genMethodName, ++value);
if (value > 1) {
genMethodName = genMethodName + value.toString();
}
sbMethodCode.append(genMethodName);
} else {
writeSubresourceMethod(resourceEl, imports, sbMethodCode, info, id, suffixName);
}
sbMethodCode.append("(");
List<Element> inParamElements = getParameters(resourceEl, info.getInheritedParams(), !isRoot && !isResourceElement && resourceEl.getAttribute("id").length() > 0);
Element repElement = getActualRepElement(allRequestReps, requestRepWithElement);
writeRequestTypes(firstRequestEl, classPackage, repElement, inParamElements, jaxpSourceRequired, sbMethodCode, sbMethodDocs, imports, info, suspendedAsync);
sbMethodCode.append(")");
if (info.isInterfaceGenerated()) {
sbMethodCode.append(";");
} else {
generateEmptyMethodBody(sbMethodCode, responseTypeAvailable);
}
sbMethodCode.append(getLineSep()).append(getLineSep());
}
finalizeMethodDocs(doCreateJavaDocs, sbCode, sbMethodDocs, sbMethodRespDocs, sbMethodCode);
}
use of javax.ws.rs.core.PathSegment in project cxf by apache.
the class JAXRSUtilsTest method testMatrixAndPathSegmentParameters.
@Test
public void testMatrixAndPathSegmentParameters() throws Exception {
Class<?>[] argType = { PathSegment.class, String.class };
Method m = Customer.class.getMethod("testPathSegment", argType);
Message messageImpl = createMessage();
messageImpl.put(Message.REQUEST_URI, "/bar%20foo;p4=0%201");
MultivaluedMap<String, String> values = new MetadataMap<String, String>();
values.add("ps", "bar%20foo;p4=0%201");
List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), values, messageImpl);
assertEquals("2 params should've been identified", 2, params.size());
PathSegment ps = (PathSegment) params.get(0);
assertEquals("bar foo", ps.getPath());
assertEquals(1, ps.getMatrixParameters().size());
assertEquals("0 1", ps.getMatrixParameters().getFirst("p4"));
assertEquals("bar foo", params.get(1));
}
use of javax.ws.rs.core.PathSegment in project cxf by apache.
the class JAXRSUtils method processMatrixParam.
private static Object processMatrixParam(Message m, String key, Class<?> pClass, Type genericType, Annotation[] paramAnns, String defaultValue, boolean decode) {
List<PathSegment> segments = JAXRSUtils.getPathSegments((String) m.get(Message.REQUEST_URI), decode);
if (!segments.isEmpty()) {
MultivaluedMap<String, String> params = new MetadataMap<String, String>();
for (PathSegment ps : segments) {
MultivaluedMap<String, String> matrix = ps.getMatrixParameters();
for (Map.Entry<String, List<String>> entry : matrix.entrySet()) {
for (String value : entry.getValue()) {
params.add(entry.getKey(), value);
}
}
}
if ("".equals(key)) {
return InjectionUtils.handleBean(pClass, paramAnns, params, ParameterType.MATRIX, m, false);
}
List<String> values = params.get(key);
return InjectionUtils.createParameterObject(values, pClass, genericType, paramAnns, defaultValue, false, ParameterType.MATRIX, m);
}
return null;
}
use of javax.ws.rs.core.PathSegment in project cxf by apache.
the class PathSegmentImplTest method testPlainPathSegment.
@Test
public void testPlainPathSegment() {
PathSegment ps = new PathSegmentImpl("bar");
assertEquals("bar", ps.getPath());
assertEquals(0, ps.getMatrixParameters().size());
}
use of javax.ws.rs.core.PathSegment in project cxf by apache.
the class PathSegmentImplTest method testPathSegmentWithDecodedMatrixParams.
@Test
public void testPathSegmentWithDecodedMatrixParams() {
PathSegment ps = new PathSegmentImpl("bar%20foo;a=1%202");
assertEquals("bar foo", ps.getPath());
MultivaluedMap<String, String> params = ps.getMatrixParameters();
assertEquals(1, params.size());
assertEquals(1, params.get("a").size());
assertEquals("1 2", params.get("a").get(0));
}
Aggregations