Search in sources :

Example 1 with XMLSource

use of org.apache.cxf.jaxrs.ext.xml.XMLSource in project ddf by codice.

the class EndpointOperationInfoResourceComparator method compare.

@Override
public int compare(OperationResourceInfo oper1, OperationResourceInfo oper2, Message message) {
    if (null == oper1 || null == oper2 || null == message) {
        LOGGER.debug("Found NULL parameters in the compare method.");
        return -1;
    }
    String httpMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
    LOGGER.debug("HTTP METHOD = {}", httpMethod);
    String requestName = null;
    String requestedService = null;
    if (HTTP_GET.equalsIgnoreCase(httpMethod)) {
        String queryString = (String) message.get(Message.QUERY_STRING);
        if (StringUtils.isNotEmpty(queryString)) {
            MultivaluedMap<String, String> allQueryParams = JAXRSUtils.getStructuredParams(queryString, QUERY_PARAM_DELIMITER, false, false);
            // Loop through the keys and do a case insensitive check
            for (Entry<String, List<String>> queryParam : allQueryParams.entrySet()) {
                if ((REQUEST_PARAM.equalsIgnoreCase(queryParam.getKey())) && (!queryParam.getValue().isEmpty()) && requestName == null) {
                    // We should never have more than one "request" query
                    // param so ignore them if we do
                    requestName = queryParam.getValue().get(0);
                    LOGGER.debug("Request Query Param = {}", requestName);
                }
                if ((SERVICE_PARAM.equalsIgnoreCase(queryParam.getKey())) && (!queryParam.getValue().isEmpty()) && requestedService == null) {
                    // We should never have more than one "service" query
                    // param so ignore them if we do
                    requestedService = queryParam.getValue().get(0);
                    LOGGER.debug("Service Query Param = {}", requestedService);
                }
            }
        }
    } else if (HTTP_POST.equalsIgnoreCase(httpMethod)) {
        // Get the payload
        try (InputStream is = message.getContent(InputStream.class)) {
            if (is != null) {
                try (CachedOutputStream bos = new CachedOutputStream()) {
                    // We need to make a copy and put it back for later
                    // processing
                    IOUtils.copy(is, bos);
                    bos.flush();
                    is.close();
                    message.setContent(InputStream.class, bos.getInputStream());
                    XMLSource xml = new XMLSource(bos.getInputStream());
                    xml.setBuffering();
                    // The request name will be the root node name
                    requestName = xml.getValue("local-name(/*)");
                    requestedService = xml.getValue("/*/@service");
                    LOGGER.debug("ROOT NODE = {}", requestName);
                    LOGGER.debug("Service Name = {}", requestedService);
                }
            }
        } catch (IOException ioe) {
            LOGGER.info("Unable to read message contents", ioe);
        }
    } else {
        LOGGER.info("Got unknown HTTP Method {}", httpMethod);
        return -1;
    }
    int op1Rank = getOperationRank(oper1, requestName, requestedService);
    int op2Rank = getOperationRank(oper2, requestName, requestedService);
    if (op1Rank == -1 && op2Rank == -1) {
        return -1;
    }
    return (op1Rank == op2Rank) ? 0 : (op1Rank < op2Rank) ? 1 : -1;
}
Also used : InputStream(java.io.InputStream) List(java.util.List) IOException(java.io.IOException) XMLSource(org.apache.cxf.jaxrs.ext.xml.XMLSource) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1 XMLSource (org.apache.cxf.jaxrs.ext.xml.XMLSource)1