Search in sources :

Example 21 with PathSegment

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);
}
Also used : Oneway(org.apache.cxf.jaxrs.ext.Oneway) Element(org.w3c.dom.Element) PathSegment(javax.ws.rs.core.PathSegment) LinkedList(java.util.LinkedList)

Example 22 with PathSegment

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));
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Example 23 with PathSegment

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;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) PathSegment(javax.ws.rs.core.PathSegment) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 24 with PathSegment

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());
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Example 25 with PathSegment

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));
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) Test(org.junit.Test)

Aggregations

PathSegment (javax.ws.rs.core.PathSegment)33 Test (org.junit.Test)13 UriInfo (javax.ws.rs.core.UriInfo)10 Map (java.util.Map)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 URI (java.net.URI)4 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 Response (javax.ws.rs.core.Response)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 Access (io.druid.server.security.Access)3 AuthorizationInfo (io.druid.server.security.AuthorizationInfo)3 Resource (io.druid.server.security.Resource)3 TreeMap (java.util.TreeMap)3 Produces (javax.ws.rs.Produces)3 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3