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 2013/07/04 12:04:36 UTC

svn commit: r1499710 - in /cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider: AbstractCachingMessageProvider.java CachingMessageBodyReader.java CachingMessageBodyWriter.java

Author: sergeyb
Date: Thu Jul  4 10:04:36 2013
New Revision: 1499710

URL: http://svn.apache.org/r1499710
Log:
[CXF-5069] Adding basic caching JAX-RS providers

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java   (with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java   (with props)
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java   (with props)

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java?rev=1499710&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java Thu Jul  4 10:04:36 2013
@@ -0,0 +1,62 @@
+/**
+ * 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.util.ResourceBundle;
+import java.util.logging.Logger;
+
+import javax.ws.rs.core.Context;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxrs.ext.MessageContext;
+
+@Provider
+public class AbstractCachingMessageProvider<T> extends AbstractConfigurableProvider {
+    
+    protected static final Logger LOG = LogUtils.getL7dLogger(AbstractCachingMessageProvider.class);
+    protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractCachingMessageProvider.class);
+    protected static final String ACTIVE_JAXRS_PROVIDER_KEY = "active.jaxrs.provider";
+    
+    protected MessageContext mc;
+    private T object;
+    
+    @Context
+    public void setMessageContext(MessageContext context) {
+        this.mc = context;
+    }
+    
+    
+    protected boolean isProviderKeyNotSet() {
+        return mc.get(ACTIVE_JAXRS_PROVIDER_KEY) == null;
+    }
+
+    public T getObject() {
+        return object;
+    }
+
+    public void setObject(T object) {
+        this.object = object;
+    }
+    
+    
+        
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractCachingMessageProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java?rev=1499710&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java Thu Jul  4 10:04:36 2013
@@ -0,0 +1,82 @@
+/**
+ * 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 javax.ws.rs.NotAcceptableException;
+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;
+
+@Provider
+public class CachingMessageBodyReader<T> extends AbstractCachingMessageProvider<T>
+    implements MessageBodyReader<T> {
+    
+    private MessageBodyReader<T> delegatingReader;
+    
+    public boolean isReadable(Class<?> type, Type gType, Annotation[] anns, MediaType mt) {
+        if (delegatingReader != null) {
+            return delegatingReader.isReadable(type, gType, anns, mt);
+        } else {
+            return isProviderKeyNotSet();
+        }
+    }
+
+    
+    public T readFrom(Class<T> type, Type gType, Annotation[] anns, MediaType mt,
+                        MultivaluedMap<String, String> theheaders, InputStream is) 
+        throws IOException, WebApplicationException {
+        this.setObject(
+            getReader(type, gType, anns, mt).readFrom(type, gType, anns, mt, theheaders, is));
+        return getObject();
+    }
+    
+    
+    protected MessageBodyReader<T> getReader(Class<?> type, Type gType, Annotation[] anns, MediaType mt) {
+        if (delegatingReader != null) {
+            return delegatingReader;
+        }
+        MessageBodyReader<T> r = null;
+        
+        mc.put(ACTIVE_JAXRS_PROVIDER_KEY, this);
+        try {
+            @SuppressWarnings("unchecked")
+            Class<T> actualType = (Class<T>)type;
+            r = mc.getProviders().getMessageBodyReader(actualType, gType, anns, mt);
+        } finally {
+            mc.put(ACTIVE_JAXRS_PROVIDER_KEY, null); 
+        }
+        
+        if (r == null) {
+            org.apache.cxf.common.i18n.Message message = 
+                new org.apache.cxf.common.i18n.Message("NO_MSG_READER", BUNDLE, type);
+            LOG.severe(message.toString());
+            throw new NotAcceptableException();
+        }
+        return r;
+    }
+
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyReader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java?rev=1499710&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java Thu Jul  4 10:04:36 2013
@@ -0,0 +1,84 @@
+/**
+ * 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.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+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.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+@Provider
+public class CachingMessageBodyWriter<T> extends AbstractCachingMessageProvider<T>
+    implements MessageBodyWriter<T> {
+    
+    private MessageBodyWriter<T> delegatingWriter;
+    
+    public long getSize(T t, Class<?> type, Type gType, Annotation[] anns, MediaType mediaType) {
+        return -1;
+    }
+
+    public boolean isWriteable(Class<?> type, Type gType, Annotation[] anns, MediaType mt) {
+        if (delegatingWriter != null) {
+            return delegatingWriter.isWriteable(type, gType, anns, mt);
+        } else {
+            return isProviderKeyNotSet();
+        }
+    }
+
+    
+    public void writeTo(T obj, Class<?> type, Type gType, Annotation[] anns, MediaType mt,
+                        MultivaluedMap<String, Object> theheaders, OutputStream os) 
+        throws IOException, WebApplicationException {
+        this.setObject(obj);
+        getWriter(type, gType, anns, mt).writeTo(getObject(), type, gType, anns, mt, theheaders, os);
+    }
+    
+    
+    protected MessageBodyWriter<T> getWriter(Class<?> type, Type gType, Annotation[] anns, MediaType mt) {
+        if (delegatingWriter != null) {
+            return delegatingWriter;
+        }
+        MessageBodyWriter<T> w = null;
+        
+        mc.put(ACTIVE_JAXRS_PROVIDER_KEY, this);
+        try {
+            @SuppressWarnings("unchecked")
+            Class<T> actualType = (Class<T>)type;
+            w = mc.getProviders().getMessageBodyWriter(actualType, gType, anns, mt);
+        } finally {
+            mc.put(ACTIVE_JAXRS_PROVIDER_KEY, null); 
+        }
+        
+        if (w == null) {
+            org.apache.cxf.common.i18n.Message message = 
+                new org.apache.cxf.common.i18n.Message("NO_MSG_WRITER", BUNDLE, type);
+            LOG.severe(message.toString());
+            throw new InternalServerErrorException();
+        }
+        return w;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/CachingMessageBodyWriter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date