You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2008/07/01 20:41:29 UTC
svn commit: r673167 [3/6] - in /cxf/trunk: parent/ rt/frontend/jaxrs/
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend...
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AtomFeedProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AtomFeedProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AtomFeedProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AtomFeedProvider.java Tue Jul 1 11:41:24 2008
@@ -22,6 +22,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.ProduceMime;
@@ -45,12 +47,16 @@
private static final String JSON_TYPE = "application/json";
private static final Abdera ATOM_ENGINE = new Abdera();
- public boolean isWriteable(Class<?> type) {
+ public long getSize(Feed feed) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
return Feed.class.isAssignableFrom(type);
}
- public void writeTo(Feed feed, MediaType mt,
- MultivaluedMap<String, Object> headers, OutputStream os)
+ public void writeTo(Feed feed, Class<?> clazz, Type type, Annotation[] a,
+ MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os)
throws IOException {
if (JSON_TYPE.equals(mt.toString())) {
Writer w = ATOM_ENGINE.getWriterFactory().getWriter("json");
@@ -58,18 +64,16 @@
} else {
feed.writeTo(os);
}
- }
-
- public long getSize(Feed feed) {
- return -1;
+
}
- public boolean isReadable(Class<?> type) {
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
return Feed.class.isAssignableFrom(type);
}
- public Feed readFrom(Class<Feed> type, MediaType mediaType,
- MultivaluedMap<String, String> headers, InputStream is) throws IOException {
+ public Feed readFrom(Class<Feed> clazz, Type t, Annotation[] a, MediaType mt,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
Document<Feed> doc = ATOM_ENGINE.getParser().parse(is);
return doc.getRoot();
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/BinaryDataProvider.java Tue Jul 1 11:41:24 2008
@@ -24,10 +24,17 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
@@ -35,20 +42,25 @@
public class BinaryDataProvider
implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
+
+ private static final int BUFFER_SIZE = 4096;
- public boolean isReadable(Class<?> type) {
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
return byte[].class.isAssignableFrom(type)
- || InputStream.class.isAssignableFrom(type);
+ || InputStream.class.isAssignableFrom(type)
+ || Reader.class.isAssignableFrom(type);
}
- public Object readFrom(Class<Object> clazz, MediaType type,
+ public Object readFrom(Class<Object> clazz, Type genericType, Annotation[] annotations, MediaType type,
MultivaluedMap<String, String> headers, InputStream is)
throws IOException {
if (InputStream.class.isAssignableFrom(clazz)) {
return is;
}
+ if (Reader.class.isAssignableFrom(clazz)) {
+ return new InputStreamReader(is, getEncoding(type));
+ }
if (byte[].class.isAssignableFrom(clazz)) {
- // lets worry about the optimization later
return IOUtils.readBytesFromStream(is);
}
throw new IOException("Unrecognized class");
@@ -61,16 +73,17 @@
return -1;
}
- public boolean isWriteable(Class<?> type) {
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
return byte[].class.isAssignableFrom(type)
|| InputStream.class.isAssignableFrom(type)
- || File.class.isAssignableFrom(type);
+ || File.class.isAssignableFrom(type)
+ || Reader.class.isAssignableFrom(type)
+ || StreamingOutput.class.isAssignableFrom(type);
}
- public void writeTo(Object o, MediaType type,
- MultivaluedMap<String, Object> headers, OutputStream os)
+ public void writeTo(Object o, Class<?> clazz, Type genericType, Annotation[] annotations,
+ MediaType type, MultivaluedMap<String, Object> headers, OutputStream os)
throws IOException {
- // TODO : use media types properly here
if (InputStream.class.isAssignableFrom(o.getClass())) {
IOUtils.copyAndCloseInput((InputStream)o, os);
@@ -78,12 +91,30 @@
IOUtils.copyAndCloseInput(new BufferedInputStream(
new FileInputStream((File)o)), os);
} else if (byte[].class.isAssignableFrom(o.getClass())) {
- // lets worry about the optimization later
os.write((byte[])o);
+ } else if (Reader.class.isAssignableFrom(o.getClass())) {
+ try {
+ Writer writer = new OutputStreamWriter(os, getEncoding(type));
+ IOUtils.copy((Reader)o,
+ writer,
+ BUFFER_SIZE);
+ writer.flush();
+ } finally {
+ ((Reader)o).close();
+ }
+
+ } else if (StreamingOutput.class.isAssignableFrom(o.getClass())) {
+ ((StreamingOutput)o).write(os);
} else {
throw new IOException("Unrecognized class");
}
}
+
+ private String getEncoding(MediaType mt) {
+ String enc = mt.getParameters().get("charset");
+ return enc == null ? "UTF-8" : enc;
+ }
+
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingReaderProvider.java Tue Jul 1 11:41:24 2008
@@ -23,6 +23,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
@@ -33,18 +35,21 @@
import javax.ws.rs.ext.Provider;
import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.jaxrs.MetadataMap;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
@ConsumeMime("application/x-www-form-urlencoded")
@Provider
public final class FormEncodingReaderProvider implements MessageBodyReader<Object> {
- public boolean isReadable(Class<?> type) {
+ public boolean isReadable(Class<?> type, Type genericType,
+ Annotation[] annotations) {
return type.isAssignableFrom(MultivaluedMap.class);
}
- public MultivaluedMap<String, String> readFrom(Class<Object> type, MediaType m,
- MultivaluedMap<String, String> headers, InputStream is) {
+ public MultivaluedMap<String, String> readFrom(
+ Class<Object> clazz, Type genericType, Annotation[] annotations, MediaType type,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
try {
String charset = "UTF-8";
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Tue Jul 1 11:41:24 2008
@@ -20,46 +20,40 @@
package org.apache.cxf.jaxrs.provider;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.Map;
-import java.util.WeakHashMap;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
-import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.transform.stream.StreamSource;
@Provider
-public final class JAXBElementProvider
- implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
-
- static Map<Class, JAXBContext> jaxbContexts = new WeakHashMap<Class, JAXBContext>();
-
- public boolean isWriteable(Class<?> type) {
- return type.getAnnotation(XmlRootElement.class) != null;
- }
+public final class JAXBElementProvider extends AbstractJAXBProvider {
- public boolean isReadable(Class<?> type) {
- return type.getAnnotation(XmlRootElement.class) != null;
- }
-
- public long getSize(Object o) {
- return -1;
- }
- public Object readFrom(Class<Object> type, MediaType m, MultivaluedMap<String, String> headers,
- InputStream is) {
+ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
try {
- JAXBContext context = getJAXBContext(type);
+ Class<?> theType = getActualType(type, genericType);
+ JAXBContext context = getJAXBContext(theType, genericType);
+
Unmarshaller unmarshaller = context.createUnmarshaller();
- return unmarshaller.unmarshal(is);
+ if (JAXBElement.class.isAssignableFrom(type)) {
+ return unmarshaller.unmarshal(new StreamSource(is), theType);
+ } else {
+ return unmarshaller.unmarshal(is);
+ }
+
} catch (JAXBException e) {
e.printStackTrace();
}
@@ -67,29 +61,25 @@
return null;
}
- public void writeTo(Object obj, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
+
+ public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[] anns,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
+ throws IOException {
try {
- JAXBContext context = getJAXBContext(obj.getClass());
- Marshaller marshaller = context.createMarshaller();
- marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
- marshaller.marshal(obj, os);
+ Object actualObject = checkAdapter(obj, anns);
+ Class<?> actualClass = actualObject.getClass();
+ if (cls == genericType) {
+ genericType = actualClass;
+ }
+ Marshaller ms = createMarshaller(actualObject, actualClass, genericType, m);
+ ms.marshal(actualObject, os);
- //TODO: support Calendar type
- //}
} catch (JAXBException e) {
//TODO: better exception handling
e.printStackTrace();
}
}
- private JAXBContext getJAXBContext(Class type) throws JAXBException {
- synchronized (jaxbContexts) {
- JAXBContext context = jaxbContexts.get(type);
- if (context == null) {
- context = JAXBContext.newInstance(type);
- jaxbContexts.put(type, context);
- }
- return context;
- }
- }
+
+
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java Tue Jul 1 11:41:24 2008
@@ -20,24 +20,24 @@
package org.apache.cxf.jaxrs.provider;
+import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
-import java.util.WeakHashMap;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
-import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -49,57 +49,57 @@
@ProduceMime("application/json")
@ConsumeMime("application/json")
@Provider
-public final class JSONProvider
- implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
-
- static Map<Class, JAXBContext> jaxbContexts = new WeakHashMap<Class, JAXBContext>();
-
- public boolean isWriteable(Class<?> type) {
- return type.getAnnotation(XmlRootElement.class) != null;
- }
+public final class JSONProvider extends AbstractJAXBProvider {
- public boolean isReadable(Class<?> type) {
- return type.getAnnotation(XmlRootElement.class) != null;
- }
+ private Map<String, String> namespaceMap = new HashMap<String, String>();
- public long getSize(Object o) {
- return -1;
+ public void setNamespaceMap(Map<String, String> namespaceMap) {
+ this.namespaceMap = namespaceMap;
}
- public Object readFrom(Class<Object> type, MediaType m, MultivaluedMap<String, String> headers,
- InputStream is) {
+ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
+
try {
- JAXBContext context = getJAXBContext(type);
+ Class<?> theType = getActualType(type, genericType);
+ JAXBContext context = getJAXBContext(theType, genericType);
Unmarshaller unmarshaller = context.createUnmarshaller();
- Map<String, String> nstojns = new HashMap<String, String>();
+ MappedXMLInputFactory factory = new MappedXMLInputFactory(namespaceMap);
+ XMLStreamReader xsw = factory.createXMLStreamReader(is);
+ Object response = null;
+ if (JAXBElement.class.isAssignableFrom(type)) {
+ response = unmarshaller.unmarshal(xsw, theType);
+ } else {
+ response = unmarshaller.unmarshal(xsw);
+ }
+ return response;
- MappedXMLInputFactory factory = new MappedXMLInputFactory(nstojns);
- XMLStreamReader xsw = factory.createXMLStreamReader(is);
- Object obj = unmarshaller.unmarshal(xsw);
- xsw.close();
- return obj;
} catch (JAXBException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
- }
+ }
return null;
}
- public void writeTo(Object obj, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
+ public void writeTo(Object obj, Class<?> cls, Type genericType, Annotation[] anns,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
+ throws IOException {
try {
- JAXBContext context = getJAXBContext(obj.getClass());
- Marshaller marshaller = context.createMarshaller();
- //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
-
- // Set up the JSON StAX implementation
- Map<String, String> nstojns = new HashMap<String, String>();
- XMLOutputFactory factory = new MappedXMLOutputFactory(nstojns);
+ Object actualObject = checkAdapter(obj, anns);
+ Class<?> actualClass = actualObject.getClass();
+ if (cls == genericType) {
+ genericType = actualClass;
+ }
+ Marshaller ms = createMarshaller(actualObject, actualClass, genericType, m);
+
+ XMLOutputFactory factory = new MappedXMLOutputFactory(namespaceMap);
XMLStreamWriter xsw = factory.createXMLStreamWriter(os);
- marshaller.marshal(obj, xsw);
+ ms.marshal(actualObject, xsw);
xsw.close();
} catch (JAXBException e) {
@@ -109,14 +109,7 @@
}
}
- private JAXBContext getJAXBContext(Class type) throws JAXBException {
- synchronized (jaxbContexts) {
- JAXBContext context = jaxbContexts.get(type);
- if (context == null) {
- context = JAXBContext.newInstance(type);
- jaxbContexts.put(type, context);
- }
- return context;
- }
- }
+
+
+
}
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java?rev=673167&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java Tue Jul 1 11:41:24 2008
@@ -0,0 +1,69 @@
+/**
+ * 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.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
+
+@ProduceMime("text/plain")
+@ConsumeMime("text/plain")
+public class PrimitiveTextProvider
+ implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
+
+ private static boolean isSupported(Class<?> type) {
+ return type.isPrimitive() || Number.class.isAssignableFrom(type);
+ }
+
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
+ return isSupported(type);
+ }
+
+ public Object readFrom(Class type, Type genType, Annotation[] anns, MediaType mt,
+ MultivaluedMap headers, InputStream is) throws IOException {
+ return InjectionUtils.handleParameter(
+ IOUtils.readStringFromStream(is).toString(), type);
+ }
+
+ public long getSize(Object t) {
+ return -1;
+ }
+
+ public boolean isWriteable(Class type, Type genericType, Annotation[] annotations) {
+ return isSupported(type);
+ }
+
+ public void writeTo(Object obj, Class type, Type genType, Annotation[] anns,
+ MediaType mt, MultivaluedMap headers, OutputStream os) throws IOException {
+ os.write(obj.toString().getBytes("UTF-8"));
+ }
+
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/PrimitiveTextProvider.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java Tue Jul 1 11:41:24 2008
@@ -19,6 +19,9 @@
package org.apache.cxf.jaxrs.provider;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -27,103 +30,220 @@
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
-import org.apache.cxf.jaxrs.JAXRSUtils;
+import org.apache.cxf.jaxrs.ext.MappingsHandler;
+import org.apache.cxf.jaxrs.ext.RequestHandler;
+import org.apache.cxf.jaxrs.ext.ResponseHandler;
+import org.apache.cxf.jaxrs.impl.RequestPreprocessor;
+import org.apache.cxf.jaxrs.model.ProviderInfo;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.message.Message;
public final class ProviderFactory {
private static final ProviderFactory PF = new ProviderFactory();
- private List<MessageBodyReader> defaultMessageReaders = new ArrayList<MessageBodyReader>();
- private List<MessageBodyWriter> defaultMessageWriters = new ArrayList<MessageBodyWriter>();
- private List<MessageBodyReader> userMessageReaders = new ArrayList<MessageBodyReader>();
- private List<MessageBodyWriter> userMessageWriters = new ArrayList<MessageBodyWriter>();
- private List<SystemQueryHandler> queryHandlers = new ArrayList<SystemQueryHandler>();
+ private List<ProviderInfo<MessageBodyReader>> defaultMessageReaders =
+ new ArrayList<ProviderInfo<MessageBodyReader>>();
+ private List<ProviderInfo<MessageBodyWriter>> defaultMessageWriters =
+ new ArrayList<ProviderInfo<MessageBodyWriter>>();
+ private List<ProviderInfo<MessageBodyReader>> userMessageReaders =
+ new ArrayList<ProviderInfo<MessageBodyReader>>();
+ private List<ProviderInfo<MessageBodyWriter>> userMessageWriters =
+ new ArrayList<ProviderInfo<MessageBodyWriter>>();
+ private List<ProviderInfo<ContextResolver>> userContextResolvers =
+ new ArrayList<ProviderInfo<ContextResolver>>();
+ private List<ProviderInfo<ExceptionMapper>> userExceptionMappers =
+ new ArrayList<ProviderInfo<ExceptionMapper>>();
+ private List<ProviderInfo<RequestHandler>> requestHandlers =
+ new ArrayList<ProviderInfo<RequestHandler>>();
+ private List<ProviderInfo<ResponseHandler>> responseHandlers =
+ new ArrayList<ProviderInfo<ResponseHandler>>();
+ private RequestPreprocessor requestPreprocessor;
private ProviderFactory() {
// TODO : this needs to be done differently,
// we need to use cxf-jaxrs-extensions
setProviders(defaultMessageReaders,
defaultMessageWriters,
+ userContextResolvers,
+ requestHandlers,
+ responseHandlers,
+ userExceptionMappers,
+ new JAXBElementProvider(),
new JSONProvider(),
new BinaryDataProvider(),
- new JAXBElementProvider(),
new StringProvider(),
new SourceProvider(),
- new AtomFeedProvider(),
- new AtomEntryProvider(),
- new FormEncodingReaderProvider());
-
- queryHandlers.add(new AcceptTypeQueryHandler());
+ new FormEncodingReaderProvider(),
+ new PrimitiveTextProvider(),
+ new MappingsHandler());
}
public static ProviderFactory getInstance() {
return PF;
}
- public <T> MessageBodyReader<T> createMessageBodyReader(Class<T> bodyType, MediaType mediaType) {
+ @SuppressWarnings("unchecked")
+ public <T> ContextResolver<T> createContextResolver(Type contextType, Message m) {
+ for (ProviderInfo<ContextResolver> cr : userContextResolvers) {
+ Type[] types = cr.getProvider().getClass().getGenericInterfaces();
+ for (Type t : types) {
+ if (t instanceof ParameterizedType) {
+ ParameterizedType pt = (ParameterizedType)t;
+ Type[] args = pt.getActualTypeArguments();
+ for (int i = 0; i < args.length; i++) {
+ if (contextType == args[i]) {
+
+ InjectionUtils.injectContextFields(cr.getProvider(), cr, m);
+ InjectionUtils.injectContextMethods(cr.getProvider(), cr, m);
+ return cr.getProvider();
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> ExceptionMapper<T> createExceptionMapper(Class<?> exceptionType, Message m) {
+ for (ProviderInfo<ExceptionMapper> em : userExceptionMappers) {
+ Type[] types = em.getProvider().getClass().getGenericInterfaces();
+ for (Type t : types) {
+ if (t instanceof ParameterizedType) {
+ ParameterizedType pt = (ParameterizedType)t;
+ Type[] args = pt.getActualTypeArguments();
+ for (int i = 0; i < args.length; i++) {
+ if (exceptionType.isAssignableFrom((Class<?>)args[i])) {
+ InjectionUtils.injectContextFields(em.getProvider(), em, m);
+ InjectionUtils.injectContextMethods(em.getProvider(), em, m);
+ return em.getProvider();
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ public <T> MessageBodyReader<T> createMessageBodyReader(Class<T> bodyType,
+ Type parameterType,
+ Annotation[] parameterAnnotations,
+ MediaType mediaType,
+ Message m) {
// Try user provided providers
MessageBodyReader<T> mr = chooseMessageReader(userMessageReaders,
bodyType,
- mediaType);
+ parameterType,
+ parameterAnnotations,
+ mediaType,
+ m);
//If none found try the default ones
if (mr == null) {
mr = chooseMessageReader(defaultMessageReaders,
bodyType,
- mediaType);
+ parameterType,
+ parameterAnnotations,
+ mediaType,
+ m);
}
return mr;
}
- public SystemQueryHandler getQueryHandler(MultivaluedMap<String, String> query) {
+
+
+ public List<ProviderInfo<RequestHandler>> getRequestHandlers() {
- for (SystemQueryHandler h : queryHandlers) {
- if (h.supports(query)) {
- return h;
- }
- }
+ return Collections.unmodifiableList(requestHandlers);
+ }
+
+ public List<ProviderInfo<ResponseHandler>> getResponseHandlers() {
- return null;
+ return Collections.unmodifiableList(responseHandlers);
}
- public <T> MessageBodyWriter<T> createMessageBodyWriter(Class<T> bodyType, MediaType mediaType) {
+ public <T> MessageBodyWriter<T> createMessageBodyWriter(Class<T> bodyType,
+ Type parameterType,
+ Annotation[] parameterAnnotations,
+ MediaType mediaType,
+ Message m) {
// Try user provided providers
MessageBodyWriter<T> mw = chooseMessageWriter(userMessageWriters,
bodyType,
- mediaType);
+ parameterType,
+ parameterAnnotations,
+ mediaType,
+ m);
//If none found try the default ones
if (mw == null) {
mw = chooseMessageWriter(defaultMessageWriters,
bodyType,
- mediaType);
+ parameterType,
+ parameterAnnotations,
+ mediaType,
+ m);
}
return mw;
}
- private void setProviders(List<MessageBodyReader> readers,
- List<MessageBodyWriter> writers,
+ private void setProviders(List<ProviderInfo<MessageBodyReader>> readers,
+ List<ProviderInfo<MessageBodyWriter>> writers,
+ List<ProviderInfo<ContextResolver>> resolvers,
+ List<ProviderInfo<RequestHandler>> requestFilters,
+ List<ProviderInfo<ResponseHandler>> responseFilters,
+ List<ProviderInfo<ExceptionMapper>> excMappers,
Object... providers) {
for (Object o : providers) {
if (MessageBodyReader.class.isAssignableFrom(o.getClass())) {
- readers.add((MessageBodyReader)o);
+ readers.add(new ProviderInfo<MessageBodyReader>((MessageBodyReader)o));
}
if (MessageBodyWriter.class.isAssignableFrom(o.getClass())) {
- writers.add((MessageBodyWriter)o);
+ writers.add(new ProviderInfo<MessageBodyWriter>((MessageBodyWriter)o));
+ }
+
+ if (ContextResolver.class.isAssignableFrom(o.getClass())) {
+ resolvers.add(new ProviderInfo<ContextResolver>((ContextResolver)o));
+ }
+
+ if (RequestHandler.class.isAssignableFrom(o.getClass())) {
+ requestFilters.add(new ProviderInfo<RequestHandler>((RequestHandler)o));
+ }
+
+ if (ResponseHandler.class.isAssignableFrom(o.getClass())) {
+ responseFilters.add(new ProviderInfo<ResponseHandler>((ResponseHandler)o));
+ }
+
+ if (ExceptionMapper.class.isAssignableFrom(o.getClass())) {
+ excMappers.add(new ProviderInfo<ExceptionMapper>((ExceptionMapper)o));
}
}
sortReaders(readers);
sortWriters(writers);
+
+ injectContexts(readers, writers, resolvers, requestFilters, responseFilters, excMappers);
+ }
+
+ void injectContexts(List<?> ... providerLists) {
+ for (List<?> list : providerLists) {
+ for (Object p : list) {
+ ProviderInfo pi = (ProviderInfo)p;
+ InjectionUtils.injectContextProxies(pi, pi.getProvider());
+ }
+ }
}
/*
@@ -133,11 +253,11 @@
* provider that lists *. Quality parameter values are also used such that
* x/y;q=1.0 < x/y;q=0.7.
*/
- private void sortReaders(List<MessageBodyReader> entityProviders) {
+ private void sortReaders(List<ProviderInfo<MessageBodyReader>> entityProviders) {
Collections.sort(entityProviders, new MessageBodyReaderComparator());
}
- private void sortWriters(List<MessageBodyWriter> entityProviders) {
+ private void sortWriters(List<ProviderInfo<MessageBodyWriter>> entityProviders) {
Collections.sort(entityProviders, new MessageBodyWriterComparator());
}
@@ -153,23 +273,20 @@
* @param requestedMimeType
* @return
*/
+ @SuppressWarnings("unchecked")
private <T> MessageBodyReader<T> chooseMessageReader(
- List<MessageBodyReader> readers, Class<T> type, MediaType mediaType) {
- for (MessageBodyReader<T> ep : readers) {
-
- if (!ep.isReadable(type)) {
- continue;
- }
-
- List<MediaType> supportedMediaTypes =
- JAXRSUtils.getConsumeTypes(ep.getClass().getAnnotation(ConsumeMime.class));
-
- List<MediaType> availableMimeTypes =
- JAXRSUtils.intersectMimeTypes(Collections.singletonList(mediaType),
- supportedMediaTypes);
-
- if (availableMimeTypes.size() != 0) {
- return ep;
+ List<ProviderInfo<MessageBodyReader>> readers,
+ Class<T> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType,
+ Message m) {
+ for (ProviderInfo<MessageBodyReader> ep : readers) {
+
+ if (matchesReaderCriterias(ep.getProvider(), type, genericType, annotations, mediaType)) {
+ InjectionUtils.injectContextFields(ep.getProvider(), ep, m);
+ InjectionUtils.injectContextMethods(ep.getProvider(), ep, m);
+ return ep.getProvider();
}
}
@@ -177,6 +294,25 @@
}
+ private <T> boolean matchesReaderCriterias(MessageBodyReader<T> ep,
+ Class<T> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ if (!ep.isReadable(type, genericType, annotations)) {
+ return false;
+ }
+
+ List<MediaType> supportedMediaTypes =
+ JAXRSUtils.getConsumeTypes(ep.getClass().getAnnotation(ConsumeMime.class));
+
+ List<MediaType> availableMimeTypes =
+ JAXRSUtils.intersectMimeTypes(Collections.singletonList(mediaType),
+ supportedMediaTypes);
+
+ return availableMimeTypes.size() != 0 ? true : false;
+
+ }
/**
* Choose the first body writer provider that matches the requestedMimeType
@@ -188,21 +324,19 @@
* @param requestedMimeType
* @return
*/
+ @SuppressWarnings("unchecked")
private <T> MessageBodyWriter<T> chooseMessageWriter(
- List<MessageBodyWriter> writers, Class<T> type, MediaType mediaType) {
- for (MessageBodyWriter<T> ep : writers) {
- if (!ep.isWriteable(type)) {
- continue;
- }
- List<MediaType> supportedMediaTypes =
- JAXRSUtils.getProduceTypes(ep.getClass().getAnnotation(ProduceMime.class));
-
- List<MediaType> availableMimeTypes =
- JAXRSUtils.intersectMimeTypes(Collections.singletonList(mediaType),
- supportedMediaTypes);
-
- if (availableMimeTypes.size() != 0) {
- return ep;
+ List<ProviderInfo<MessageBodyWriter>> writers,
+ Class<T> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType,
+ Message m) {
+ for (ProviderInfo<MessageBodyWriter> ep : writers) {
+ if (matchesWriterCriterias(ep.getProvider(), type, genericType, annotations, mediaType)) {
+ InjectionUtils.injectContextFields(ep.getProvider(), ep, m);
+ InjectionUtils.injectContextMethods(ep.getProvider(), ep, m);
+ return ep.getProvider();
}
}
@@ -210,57 +344,91 @@
}
- //TODO : also scan for the @Provider annotated implementations
- public boolean registerUserEntityProvider(Object o) {
- setProviders(userMessageReaders, userMessageWriters, o);
- return true;
+ private <T> boolean matchesWriterCriterias(MessageBodyWriter<T> ep,
+ Class<T> type,
+ Type genericType,
+ Annotation[] annotations,
+ MediaType mediaType) {
+ if (!ep.isWriteable(type, genericType, annotations)) {
+ return false;
+ }
+
+ List<MediaType> supportedMediaTypes =
+ JAXRSUtils.getProduceTypes(ep.getClass().getAnnotation(ProduceMime.class));
+
+ List<MediaType> availableMimeTypes =
+ JAXRSUtils.intersectMimeTypes(Collections.singletonList(mediaType),
+ supportedMediaTypes);
+
+ return availableMimeTypes.size() != 0 ? true : false;
+
}
- public boolean deregisterUserEntityProvider(Object o) {
- boolean result = false;
+ public boolean deregisterEntityProvide(Object o) {
+
if (o instanceof MessageBodyReader) {
- result = userMessageReaders.remove(o);
+ return userMessageReaders.remove(o);
+ }
+ if (o instanceof MessageBodyWriter) {
+ return userMessageWriters.remove(o);
+ }
+ if (o instanceof ContextResolver) {
+ return userContextResolvers.remove(o);
+ }
+ if (o instanceof RequestHandler) {
+ return requestHandlers.remove(o);
}
- return o instanceof MessageBodyReader
- ? result && userMessageWriters.remove(o) : result;
+
+ return false;
}
- public List<MessageBodyReader> getDefaultMessageReaders() {
- return defaultMessageReaders;
+ List<ProviderInfo<MessageBodyReader>> getDefaultMessageReaders() {
+ return Collections.unmodifiableList(defaultMessageReaders);
}
- public List<MessageBodyWriter> getDefaultMessageWriters() {
- return defaultMessageWriters;
+ List<ProviderInfo<MessageBodyWriter>> getDefaultMessageWriters() {
+ return Collections.unmodifiableList(defaultMessageWriters);
}
- public List<MessageBodyReader> getUserMessageReaders() {
- return userMessageReaders;
+ List<ProviderInfo<MessageBodyReader>> getUserMessageReaders() {
+ return Collections.unmodifiableList(userMessageReaders);
}
- public List<MessageBodyWriter> getUserMessageWriters() {
- return userMessageWriters;
+ List<ProviderInfo<MessageBodyWriter>> getUserMessageWriters() {
+ return Collections.unmodifiableList(userMessageWriters);
}
- public void clearUserMessageProviders() {
- userMessageReaders.clear();
- userMessageWriters.clear();
+ List<ProviderInfo<ContextResolver>> getUserContextResolvers() {
+ return Collections.unmodifiableList(userContextResolvers);
+ }
+
+
+ public void registerUserProvider(Object provider) {
+ setUserProviders(Collections.singletonList(provider));
}
-
/**
* Use for injection of entityProviders
* @param entityProviders the entityProviders to set
*/
- public void setUserEntityProviders(List<?> userProviders) {
+ public void setUserProviders(List<?> userProviders) {
setProviders(userMessageReaders,
userMessageWriters,
+ userContextResolvers,
+ requestHandlers,
+ responseHandlers,
+ userExceptionMappers,
userProviders.toArray());
}
private static class MessageBodyReaderComparator
- implements Comparator<MessageBodyReader> {
+ implements Comparator<ProviderInfo<MessageBodyReader>> {
- public int compare(MessageBodyReader e1, MessageBodyReader e2) {
+ public int compare(ProviderInfo<MessageBodyReader> p1,
+ ProviderInfo<MessageBodyReader> p2) {
+ MessageBodyReader e1 = p1.getProvider();
+ MessageBodyReader e2 = p2.getProvider();
+
ConsumeMime c = e1.getClass().getAnnotation(ConsumeMime.class);
String[] mimeType1 = {"*/*"};
if (c != null) {
@@ -289,9 +457,13 @@
}
private static class MessageBodyWriterComparator
- implements Comparator<MessageBodyWriter> {
+ implements Comparator<ProviderInfo<MessageBodyWriter>> {
- public int compare(MessageBodyWriter e1, MessageBodyWriter e2) {
+ public int compare(ProviderInfo<MessageBodyWriter> p1,
+ ProviderInfo<MessageBodyWriter> p2) {
+ MessageBodyWriter e1 = p1.getProvider();
+ MessageBodyWriter e2 = p2.getProvider();
+
ProduceMime c = e1.getClass().getAnnotation(ProduceMime.class);
String[] mimeType1 = {"*/*"};
if (c != null) {
@@ -318,4 +490,41 @@
return str1.compareTo(str2);
}
}
+
+ public void setRequestPreporcessor(RequestPreprocessor rp) {
+ this.requestPreprocessor = rp;
+ }
+
+ public RequestPreprocessor getRequestPreprocessor() {
+ return requestPreprocessor;
+ }
+
+ public void cleatThreadLocalProxies() {
+ clearProxies(defaultMessageReaders,
+ defaultMessageWriters,
+ userMessageReaders,
+ userMessageWriters,
+ userContextResolvers,
+ requestHandlers,
+ responseHandlers,
+ userExceptionMappers);
+ }
+
+ void clearProxies(List<?> ...lists) {
+ for (List<?> list : lists) {
+ for (Object p : list) {
+ ProviderInfo pi = (ProviderInfo)p;
+ pi.clearThreadLocalProxies();
+ }
+ }
+ }
+
+ void clearProviders() {
+ userMessageReaders.clear();
+ userMessageWriters.clear();
+ userContextResolvers.clear();
+ userExceptionMappers.clear();
+ requestHandlers.clear();
+ responseHandlers.clear();
+ }
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/SourceProvider.java Tue Jul 1 11:41:24 2008
@@ -22,6 +22,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
@@ -44,16 +46,17 @@
public class SourceProvider implements
MessageBodyReader<Object>, MessageBodyWriter<Source> {
- public boolean isWriteable(Class<?> type) {
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
return Source.class.isAssignableFrom(type);
}
- public boolean isReadable(Class<?> type) {
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
return Source.class.isAssignableFrom(type);
}
- public Object readFrom(Class<Object> source, MediaType media,
- MultivaluedMap<String, String> httpHeaders, InputStream is) throws IOException {
+ public Object readFrom(Class<Object> source, Type genericType, Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
if (DOMSource.class.isAssignableFrom(source)) {
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -76,8 +79,9 @@
throw new IOException("Unrecognized source");
}
- public void writeTo(Source source, MediaType media, MultivaluedMap<String, Object> httpHeaders,
- OutputStream os) throws IOException {
+ public void writeTo(Source source, Class<?> clazz, Type genericType, Annotation[] annotations,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
+ throws IOException {
StreamResult result = new StreamResult(os);
TransformerFactory tf = TransformerFactory.newInstance();
try {
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/StringProvider.java Tue Jul 1 11:41:24 2008
@@ -23,6 +23,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
@@ -36,11 +38,11 @@
public final class StringProvider
implements MessageBodyWriter<String>, MessageBodyReader<String> {
- public boolean isWriteable(Class<?> type) {
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
return type == String.class;
}
- public boolean isReadable(Class<?> type) {
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
return type == String.class;
}
@@ -48,8 +50,9 @@
return s.length();
}
- public String readFrom(Class<String> type, MediaType m, MultivaluedMap<String, String> headers,
- InputStream is) {
+ public String readFrom(Class<String> clazz, Type genericType, Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
try {
return IOUtils.toString(is);
} catch (IOException e) {
@@ -58,7 +61,8 @@
return null;
}
- public void writeTo(String obj, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
+ public void writeTo(String obj, Class<?> clazz, Type genericType, Annotation[] annotations,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException {
try {
os.write(obj.getBytes());
} catch (IOException e) {
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansElementProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansElementProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansElementProvider.java Tue Jul 1 11:41:24 2008
@@ -23,8 +23,10 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
+import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import javax.ws.rs.ConsumeMime;
@@ -48,17 +50,17 @@
public class XMLBeansElementProvider implements MessageBodyReader<XmlObject>, MessageBodyWriter<XmlObject> {
/** {@inheritDoc} */
- public XmlObject readFrom(Class type, MediaType mediaType, MultivaluedMap httpHeaders,
- InputStream entityStream) throws IOException {
-
- XmlObject result = parseXmlBean(type, entityStream);
+ public XmlObject readFrom(Class<XmlObject> type, Type genericType,
+ Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
- return result;
+ return parseXmlBean(type, is);
}
/** {@inheritDoc} */
- public void writeTo(XmlObject t, MediaType mediaType, MultivaluedMap httpHeaders,
- OutputStream entityStream)
+ public void writeTo(XmlObject t, Class<?> cls, Type genericType, Annotation[] annotations,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream entityStream)
throws IOException {
// May need to set some XMLOptions here
@@ -72,12 +74,12 @@
}
/** {@inheritDoc} */
- public boolean isReadable(Class type) {
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations) {
return isXmlBean(type);
}
/** {@inheritDoc} */
- public boolean isWriteable(Class type) {
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
return isXmlBean(type);
}
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansJSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansJSONProvider.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansJSONProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XMLBeansJSONProvider.java Tue Jul 1 11:41:24 2008
@@ -23,6 +23,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
@@ -51,7 +53,11 @@
public final class XMLBeansJSONProvider extends XMLBeansElementProvider {
/** {@inheritDoc} */
- public XmlObject readFrom(Class type, MediaType m, MultivaluedMap headers, InputStream is) {
+ @Override
+ public XmlObject readFrom(Class<XmlObject> type, Type genericType,
+ Annotation[] annotations, MediaType m,
+ MultivaluedMap<String, String> headers, InputStream is)
+ throws IOException {
XmlObject result = null;
try {
@@ -74,7 +80,9 @@
}
/** {@inheritDoc} */
- public void writeTo(XmlObject obj, MediaType m, MultivaluedMap headers, OutputStream os) {
+ @Override
+ public void writeTo(XmlObject obj, Class<?> cls, Type genericType, Annotation[] annotations,
+ MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
try {
Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java?rev=673167&r1=673166&r2=673167&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/JAXRSServerFactoryBeanDefinitionParser.java Tue Jul 1 11:41:24 2008
@@ -55,9 +55,11 @@
@Override
protected void mapElement(ParserContext ctx, BeanDefinitionBuilder bean, Element el, String name) {
- if ("properties".equals(name)) {
+ if ("properties".equals(name)
+ || "extensionMappings".equals(name)
+ || "languageMappings".equals(name)) {
Map map = ctx.getDelegate().parseMapElement(el, bean.getBeanDefinition());
- bean.addPropertyValue("properties", map);
+ bean.addPropertyValue(name, map);
} else if ("executor".equals(name)) {
setFirstChildAsProperty(el, ctx, bean, "serviceFactory.executor");
} else if ("invoker".equals(name)) {
@@ -69,7 +71,7 @@
List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
bean.addPropertyValue(name, list);
} else if ("features".equals(name) || "schemaLocations".equals(name)
- || "entityProviders".equals(name) || "serviceBeans".equals(name)) {
+ || "providers".equals(name) || "serviceBeans".equals(name)) {
List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
bean.addPropertyValue(name, list);
} else {
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java?rev=673167&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java Tue Jul 1 11:41:24 2008
@@ -0,0 +1,232 @@
+/**
+ * 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.utils;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.CookieParam;
+import javax.ws.rs.Encoded;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.MatrixParam;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.MessageBodyWorkers;
+
+import org.apache.cxf.jaxrs.model.ClassResourceInfo;
+
+public final class AnnotationUtils {
+
+ private static final Set<Class> CONTEXT_CLASSES;
+ private static final Set<Class> PARAM_ANNOTATION_CLASSES;
+ private static final Set<Class> METHOD_ANNOTATION_CLASSES;
+ static {
+ CONTEXT_CLASSES = initContextClasses();
+ PARAM_ANNOTATION_CLASSES = initParamAnnotationClasses();
+ METHOD_ANNOTATION_CLASSES = initMethodAnnotationClasses();
+ }
+
+
+ private AnnotationUtils() {
+
+ }
+
+ private static Set<Class> initContextClasses() {
+ Set<Class> classes = new HashSet<Class>();
+ classes.add(UriInfo.class);
+ classes.add(SecurityContext.class);
+ classes.add(HttpHeaders.class);
+ classes.add(ContextResolver.class);
+ classes.add(MessageBodyWorkers.class);
+ classes.add(Request.class);
+ classes.add(HttpServletRequest.class);
+ classes.add(HttpServletResponse.class);
+ classes.add(ServletContext.class);
+ return classes;
+ }
+
+ private static Set<Class> initParamAnnotationClasses() {
+ Set<Class> classes = new HashSet<Class>();
+ classes.add(PathParam.class);
+ classes.add(QueryParam.class);
+ classes.add(MatrixParam.class);
+ classes.add(HeaderParam.class);
+ classes.add(CookieParam.class);
+ return classes;
+ }
+
+ private static Set<Class> initMethodAnnotationClasses() {
+ Set<Class> classes = new HashSet<Class>();
+ classes.add(HttpMethod.class);
+ classes.add(Path.class);
+ classes.add(ProduceMime.class);
+ classes.add(ConsumeMime.class);
+ return classes;
+ }
+
+ public static boolean isContextClass(Class<?> contextClass) {
+ return CONTEXT_CLASSES.contains(contextClass);
+ }
+
+ public static boolean isParamAnnotationClass(Class<?> annotationClass) {
+ return PARAM_ANNOTATION_CLASSES.contains(annotationClass);
+ }
+
+ public static boolean isMethodAnnotation(Annotation a) {
+ return METHOD_ANNOTATION_CLASSES.contains(a.annotationType())
+ || a.annotationType() == HttpMethod.class;
+ }
+
+ public static String getAnnotationValue(Annotation a) {
+ if (a.annotationType() == PathParam.class) {
+ return ((PathParam)a).value();
+ } else if (a.annotationType() == QueryParam.class) {
+ return ((QueryParam)a).value();
+ } else if (a.annotationType() == MatrixParam.class) {
+ return ((MatrixParam)a).value();
+ } else if (a.annotationType() == HeaderParam.class) {
+ return ((HeaderParam)a).value();
+ } else if (a.annotationType() == CookieParam.class) {
+ return ((CookieParam)a).value();
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> T getAnnotation(Annotation[] anns, Class<T> type) {
+ if (anns == null) {
+ return null;
+ }
+ for (Annotation a : anns) {
+ if (a.annotationType() == type) {
+ return (T)a;
+ }
+ }
+ return null;
+ }
+
+ public static Method getAnnotatedMethod(Method m) {
+ if (m == null) {
+ return m;
+ }
+ for (Annotation a : m.getAnnotations()) {
+ if (AnnotationUtils.isMethodAnnotation(a)) {
+ return m;
+ }
+ }
+ for (Annotation[] paramAnnotations : m.getParameterAnnotations()) {
+ for (Annotation a : paramAnnotations) {
+ if (AnnotationUtils.isParamAnnotationClass(a.annotationType())) {
+ return m;
+ }
+ }
+ }
+
+
+ Class<?> superC = m.getDeclaringClass().getSuperclass();
+ if (superC != null) {
+ try {
+ Method method = getAnnotatedMethod(superC.getMethod(m.getName(), m.getParameterTypes()));
+ if (method != null) {
+ return method;
+ }
+ } catch (NoSuchMethodException ex) {
+ // ignore
+ }
+ }
+ for (Class<?> i : m.getDeclaringClass().getInterfaces()) {
+ try {
+ Method method = getAnnotatedMethod(i.getMethod(m.getName(), m.getParameterTypes()));
+ if (method != null) {
+ return method;
+ }
+ } catch (NoSuchMethodException ex) {
+ // ignore
+ }
+ }
+ return m;
+ }
+
+
+
+ public static String getHttpMethodValue(Method m) {
+ for (Annotation a : m.getAnnotations()) {
+ HttpMethod httpM = a.annotationType().getAnnotation(HttpMethod.class);
+ if (httpM != null) {
+ return httpM.value();
+ }
+ }
+ return null;
+ }
+
+ public static Annotation getMethodAnnotation(Method m,
+ Class<? extends Annotation> aClass) {
+ return m == null ? null : m.getAnnotation(aClass);
+ }
+
+ public static Annotation getClassAnnotation(Class<?> c,
+ Class<? extends Annotation> aClass) {
+ if (c == null) {
+ return null;
+ }
+ Annotation p = c.getAnnotation(aClass);
+ if (p != null) {
+ return p;
+ }
+
+ p = getClassAnnotation(c.getSuperclass(), aClass);
+ if (p != null) {
+ return p;
+ }
+
+ // finally try the first one on the interface
+ for (Class<?> i : c.getInterfaces()) {
+ p = getClassAnnotation(i, aClass);
+ if (p != null) {
+ return p;
+ }
+ }
+ return null;
+ }
+
+ public static boolean isEncoded(Annotation[] anns, ClassResourceInfo cri) {
+ if (cri == null) {
+ return false;
+ }
+ if (cri.isEncodedEnabled()) {
+ return true;
+ }
+ return AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/AnnotationUtils.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=673167&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java Tue Jul 1 11:41:24 2008
@@ -0,0 +1,368 @@
+/**
+ * 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.utils;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Type;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.logging.Logger;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.MessageBodyWorkers;
+
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.util.PrimitiveUtils;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalContextResolver;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageBodyWorkers;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalRequest;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalSecurityContext;
+import org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.message.Message;
+
+public final class InjectionUtils {
+
+ private static final Logger LOG = LogUtils.getL7dLogger(InjectionUtils.class);
+ private static final ResourceBundle BUNDLE = BundleUtils.getBundle(InjectionUtils.class);
+
+ private InjectionUtils() {
+
+ }
+
+ public static Method checkProxy(Method methodToInvoke, Object resourceObject) {
+ if (Proxy.class.isInstance(resourceObject)) {
+
+ for (Class<?> c : resourceObject.getClass().getInterfaces()) {
+ try {
+ Method m = c.getMethod(
+ methodToInvoke.getName(), methodToInvoke.getParameterTypes());
+ if (m != null) {
+ return m;
+ }
+ } catch (NoSuchMethodException ex) {
+ //ignore
+ }
+ }
+
+ }
+ return methodToInvoke;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void injectFieldValue(final Field f,
+ final Object o,
+ final Object v) {
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ f.setAccessible(true);
+ try {
+ f.set(o, v);
+ } catch (IllegalAccessException ex) {
+ LOG.warning(new org.apache.cxf.common.i18n.Message("FIELD_INJECTION_FAILURE",
+ BUNDLE,
+ f.getName()).toString());
+ }
+ return null;
+ }
+ });
+
+ }
+
+ public static Class<?> getActualType(Type genericType) {
+ if (genericType == null
+ || !ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
+ return null;
+ }
+ ParameterizedType paramType = (ParameterizedType)genericType;
+ return (Class<?>)paramType.getActualTypeArguments()[0];
+ }
+
+ public static void injectThroughMethod(Object requestObject,
+ Method method,
+ Object parameterValue) {
+ try {
+ Method methodToInvoke = checkProxy(method, requestObject);
+ methodToInvoke.invoke(requestObject, new Object[]{parameterValue});
+ } catch (Exception ex) {
+ LOG.warning(new org.apache.cxf.common.i18n.Message("METHOD_INJECTION_FAILURE",
+ BUNDLE,
+ method.getName()).toString());
+ }
+ }
+
+ public static Object handleParameter(String value, Class<?> pClass) {
+ if (pClass.isPrimitive()) {
+ return PrimitiveUtils.read(value, pClass);
+ }
+ // check constructors accepting a single String value
+ try {
+ Constructor<?> c = pClass.getConstructor(new Class<?>[]{String.class});
+ if (c != null) {
+ return c.newInstance(new Object[]{value});
+ }
+ } catch (Exception ex) {
+ // try valueOf
+ }
+ // check for valueOf(String) static methods
+ try {
+ Method m = pClass.getMethod("valueOf", new Class<?>[]{String.class});
+ if (m != null && Modifier.isStatic(m.getModifiers())) {
+ return m.invoke(null, new Object[]{value});
+ }
+ } catch (Exception ex) {
+ // no luck
+ }
+ return null;
+ }
+
+ public static Object handleBean(Class<?> paramType, MultivaluedMap<String, String> values) {
+ Object bean = null;
+ try {
+ bean = paramType.newInstance();
+ for (Map.Entry<String, List<String>> entry : values.entrySet()) {
+ boolean injected = false;
+ for (Method m : paramType.getMethods()) {
+ if (m.getName().equalsIgnoreCase("set" + entry.getKey())
+ && m.getParameterTypes().length == 1) {
+ Object paramValue = handleParameter(entry.getValue().get(0),
+ m.getParameterTypes()[0]);
+ if (paramValue != null) {
+ injectThroughMethod(bean, m, paramValue);
+ injected = true;
+ break;
+ }
+ }
+ }
+ if (injected) {
+ continue;
+ }
+ for (Field f : paramType.getFields()) {
+ if (f.getName().equalsIgnoreCase(entry.getKey())) {
+ Object paramValue = handleParameter(entry.getValue().get(0),
+ f.getType());
+ if (paramValue != null) {
+ injectFieldValue(f, bean, paramValue);
+ break;
+ }
+ }
+ }
+ }
+ } catch (Exception ex) {
+ LOG.warning(new org.apache.cxf.common.i18n.Message("CLASS_INSTANCIATION_FAILURE",
+ BUNDLE,
+ paramType.getName()).toString());
+ }
+ return bean;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Object injectIntoList(Type genericType, List<String> values,
+ boolean decoded) {
+ Class<?> realType = InjectionUtils.getActualType(genericType);
+ List theValues = new ArrayList();
+ for (String r : values) {
+ if (decoded) {
+ r = JAXRSUtils.uriDecode(r);
+ }
+ Object o = InjectionUtils.handleParameter(r, realType);
+ if (o != null) {
+ theValues.add(o);
+ }
+ }
+ return theValues;
+ }
+
+
+
+ @SuppressWarnings("unchecked")
+ public static Object injectIntoSet(Type genericType, List<String> values,
+ boolean sorted, boolean decoded) {
+ Class<?> realType = InjectionUtils.getActualType(genericType);
+ Set theValues = sorted ? new TreeSet() : new HashSet();
+ for (String r : values) {
+ if (decoded) {
+ r = JAXRSUtils.uriDecode(r);
+ }
+ Object o = InjectionUtils.handleParameter(r, realType);
+ if (o != null) {
+ theValues.add(o);
+ }
+ }
+ return theValues;
+ }
+
+ public static Object createParameterObject(List<String> paramValues,
+ Class<?> paramType,
+ Type genericType,
+ String defaultValue,
+ boolean isLast,
+ boolean decoded) {
+
+ if (paramValues == null) {
+ if (defaultValue != null) {
+ paramValues = Collections.singletonList(defaultValue);
+ } else {
+ return null;
+ }
+ }
+
+ if (List.class.isAssignableFrom(paramType)) {
+ return InjectionUtils.injectIntoList(genericType, paramValues, decoded);
+ } else if (Set.class.isAssignableFrom(paramType)) {
+ return InjectionUtils.injectIntoSet(genericType, paramValues, false, decoded);
+ } else if (SortedSet.class.isAssignableFrom(paramType)) {
+ return InjectionUtils.injectIntoSet(genericType, paramValues, true, decoded);
+ } else {
+ String result = null;
+ if (paramValues.size() > 0) {
+ result = isLast ? paramValues.get(paramValues.size() - 1)
+ : paramValues.get(0);
+ }
+ if (result != null) {
+ if (decoded) {
+ result = JAXRSUtils.uriDecode(result);
+ }
+ return InjectionUtils.handleParameter(result, paramType);
+ } else {
+ return null;
+ }
+ }
+ }
+
+ public static ThreadLocalProxy createThreadLocalProxy(Class<?> type) {
+ ThreadLocalProxy proxy = null;
+ if (UriInfo.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalUriInfo();
+ } else if (HttpHeaders.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalHttpHeaders();
+ } else if (SecurityContext.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalSecurityContext();
+ } else if (ContextResolver.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalContextResolver();
+ } else if (Request.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalRequest();
+ } else if (MessageBodyWorkers.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalMessageBodyWorkers();
+ } else if (MessageBodyWorkers.class.isAssignableFrom(type)) {
+ proxy = new ThreadLocalHttpServletRequest();
+ }
+ return proxy;
+ }
+
+ public static void injectContextProxies(AbstractResourceInfo cri, Object instance) {
+ if (!cri.isSingleton()) {
+ return;
+ }
+
+ for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet()) {
+ ThreadLocalProxy proxy = cri.getContextSetterProxy(entry.getValue());
+ InjectionUtils.injectThroughMethod(instance, entry.getValue(), proxy);
+ }
+
+ for (Field f : cri.getContextFields()) {
+ ThreadLocalProxy proxy = cri.getContextFieldProxy(f);
+ InjectionUtils.injectFieldValue(f, instance, proxy);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void injectContextField(AbstractResourceInfo cri,
+ Field f, Object o, Object value,
+ boolean resource) {
+ if (!cri.isSingleton()) {
+ InjectionUtils.injectFieldValue(f, o, value);
+ } else {
+ ThreadLocalProxy proxy = resource ? cri.getResourceFieldProxy(f)
+ : cri.getContextFieldProxy(f);
+ if (proxy != null) {
+ proxy.set(value);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static void injectContextMethods(Object requestObject,
+ AbstractResourceInfo cri,
+ Message message) {
+ for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet()) {
+ Object o = JAXRSUtils.createContextValue(message,
+ entry.getValue().getGenericParameterTypes()[0],
+ entry.getKey());
+
+ if (o != null) {
+ if (!cri.isSingleton()) {
+ InjectionUtils.injectThroughMethod(requestObject, entry.getValue(), o);
+ } else {
+ ThreadLocalProxy proxy = cri.getContextSetterProxy(entry.getValue());
+ if (proxy != null) {
+ proxy.set(o);
+ }
+ }
+
+ }
+ }
+ }
+
+ // TODO : should we have context and resource fields be treated as context fields ?
+
+ public static void injectContextFields(Object o,
+ AbstractResourceInfo cri,
+ Message m) {
+
+ for (Field f : cri.getContextFields()) {
+ Object value = JAXRSUtils.createContextValue(m, f.getGenericType(), f.getType());
+ InjectionUtils.injectContextField(cri, f, o, value, false);
+ }
+ }
+
+ public static void injectResourceFields(Object o,
+ AbstractResourceInfo cri,
+ Message m) {
+
+ for (Field f : cri.getResourceFields()) {
+ Object value = JAXRSUtils.createResourceValue(m, f.getType());
+ InjectionUtils.injectContextField(cri, f, o, value, true);
+ }
+ }
+}
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
------------------------------------------------------------------------------
svn:keywords = Rev Date