All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.jaxrs.provider.XPathProvider Maven / Gradle / Ivy

There is a newer version: 3.0.0-milestone2
Show newest version
/**
 * 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.jaxrs.provider;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import org.apache.cxf.jaxrs.ext.xml.XMLSource;

@Provider
@Consumes({"text/xml", "application/xml", "application/*+xml" })
public class XPathProvider implements MessageBodyReader {

    private List consumeMediaTypes;
    private Map classExpressions;
    private String globalExpression;
    private String className;
    private boolean forceDOM;
    private Map globalNamespaces = 
        Collections.emptyMap();
    
    public void setConsumeMediaTypes(List types) {
        consumeMediaTypes = types;
    }
    
    public List getConsumeMediaTypes() {
        return consumeMediaTypes;    
    }
    
    public void setForceDOM(boolean b) {
        forceDOM = b;
    }

    public void setExpression(String expr) {
        globalExpression = expr;
    }
    
    public void setClassName(String name) {
        className = name;
    }
    
    public void setExpressions(Map expressions) {
        classExpressions = expressions;
    }
    
    public void setNamespaces(Map nsMap) {
        globalNamespaces = nsMap;
    }
    
    public boolean isReadable(Class cls, Type genericType, Annotation[] annotations, MediaType mediaType) {
        
        return globalExpression != null && (className == null 
            || className.equals(cls.getName()))  
            || classExpressions != null && classExpressions.containsKey(cls.getName());
    }

    public T readFrom(Class cls, Type type, Annotation[] anns, MediaType mt, 
        MultivaluedMap hrs, InputStream is) throws IOException, WebApplicationException {
        String expression = globalExpression != null ? globalExpression
            : classExpressions.get(cls.getName());
        if (expression == null) {
            // must not happen if isReadable() returned true
            throw new InternalServerErrorException();
        }
        XMLSource source = new XMLSource(is);
        if (forceDOM) {
            source.setBuffering();
        }
        return source.getNode(expression, globalNamespaces, cls);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy