cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject [cxf] branch master updated: Trying to share most of the code between RequestDispatcher and SpringViewResolver providers
Date Fri, 03 Nov 2017 09:49:16 GMT
This is an automated email from the ASF dual-hosted git repository.

sergeyb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ceb2e9  Trying to share most of the code between RequestDispatcher and SpringViewResolver providers
6ceb2e9 is described below

commit 6ceb2e9862ae85cb884f2dc416911eff3bf22e87
Author: Sergey Beryozkin <sberyozkin@gmail.com>
AuthorDate: Fri Nov 3 09:49:02 2017 +0000

    Trying to share most of the code between RequestDispatcher and SpringViewResolver providers
---
 .../AbstractResponseViewProvider.java}             | 403 +++++++++------------
 .../jaxrs/provider/RequestDispatcherProvider.java  | 262 ++------------
 .../jaxrs/spring/SpringViewResolverProvider.java   | 295 +--------------
 3 files changed, 208 insertions(+), 752 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractResponseViewProvider.java
similarity index 55%
copy from rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java
copy to rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractResponseViewProvider.java
index 8fa38cd..b7e4b5b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractResponseViewProvider.java
@@ -16,157 +16,115 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.jaxrs.spring;
+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 java.util.Collections;
-import java.util.Locale;
 import java.util.Map;
-import java.util.ResourceBundle;
 import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.servlet.RequestDispatcher;
+import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
-import org.apache.cxf.common.i18n.BundleUtils;
-import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
-import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
-import org.springframework.web.servlet.LocaleResolver;
-import org.springframework.web.servlet.View;
-import org.springframework.web.servlet.ViewResolver;
 
-/**
- * CXF view provider that delegates view rendering to Spring MVC Views.
- *
- * Sample usage in a spring application:
- * <pre>
- @Bean
- public SpringViewResolverProvider springViewProvider(ViewResolver viewResolver) {
-     SpringViewResolverProvider viewProvider = new SpringViewResolverProvider(viewResolver,
-            new AcceptHeaderLocaleResolver());
-     viewProvider.setUseClassNames(true);
-     viewProvider.setBeanName("model");
-     viewProvider.setResourcePaths(Collections.singletonMap("/remove", "registeredClients"));
-     return viewProvider;
- }
- * </pre>
- */
 @Produces("text/html")
 @Provider
-public class SpringViewResolverProvider extends AbstractConfigurableProvider implements MessageBodyWriter<Object> {
-
-    private static final ResourceBundle BUNDLE = BundleUtils.getBundle(SpringViewResolverProvider.class);
-
-    private static final Logger LOG = LogUtils.getL7dLogger(SpringViewResolverProvider.class);
+public abstract class AbstractResponseViewProvider extends AbstractConfigurableProvider
+    implements MessageBodyWriter<Object> {
 
     private static final String MESSAGE_RESOURCE_PATH_PROPERTY = "redirect.resource.path";
-
-    private static final String DEFAULT_RESOURCE_EXTENSION = "";
-
-    private static final String DEFAULT_LOCATION_PREFIX = "";
-
-    private final ViewResolver viewResolver;
-
+    
+    private boolean useClassNames;
+    private boolean strictPathCheck;
+    private Map<String, String> beanNames = Collections.emptyMap();
+    private String beanName;
     private String resourcePath;
-
     private Map<String, String> resourcePaths = Collections.emptyMap();
-
     private Map<String, String> classResources = Collections.emptyMap();
-
     private Map<? extends Enum<?>, String> enumResources = Collections.emptyMap();
-
-    private boolean useClassNames;
-
-    private Map<String, String> beanNames = Collections.emptyMap();
-
-    private String beanName;
-
-    private boolean logRedirects;
-
-    private boolean strictPathCheck;
-
     private String locationPrefix;
-
     private String resourceExtension;
-
-    private MessageContext mc;
-
-    private LocaleResolver localeResolver;
-
     private String errorView = "/error";
-
-    public SpringViewResolverProvider(ViewResolver viewResolver, LocaleResolver localeResolver) {
-        if (viewResolver == null) {
-            throw new IllegalArgumentException("Argument viewResolver is required");
-        }
-        if (localeResolver == null) {
-            throw new IllegalArgumentException("Argument localeResolver is required");
-        }
-        this.viewResolver = viewResolver;
-        this.localeResolver = localeResolver;
-    }
+    private boolean logRedirects;
+        
+    private MessageContext mc;
 
     @Context
     public void setMessageContext(MessageContext context) {
         this.mc = context;
     }
 
-    public void setStrictPathCheck(boolean use) {
-        strictPathCheck = use;
+    public MessageContext getMessageContext() {
+        return mc;
     }
-
+    
     public void setUseClassNames(boolean use) {
         useClassNames = use;
     }
+    
+    public boolean isUseClassNames() {
+        return useClassNames;
+    }
+    
+    public void setStrictPathCheck(boolean use) {
+        strictPathCheck = use;
+    }
+    
+    public void setBeanNames(Map<String, String> beanNames) {
+        this.beanNames = beanNames;
+    }
 
+    public void setBeanName(String beanName) {
+        this.beanName = beanName;
+    }
+    
+    public void setResourcePath(String resourcePath) {
+        this.resourcePath = resourcePath;
+    }
+    
+    public void setResourcePaths(Map<String, String> resourcePaths) {
+        this.resourcePaths = resourcePaths;
+    }
+    
     public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
         return -1;
     }
-
-    private String getViewName(Class<?> type) {
-        String viewName = doGetClassResourceName(type);
-        if (viewName == null) {
-            for (Class<?> in : type.getInterfaces()) {
-                viewName = doGetClassResourceName(in);
-                if (viewName != null) {
-                    break;
-                }
-            }
-        }
-        return viewName;
+    
+    public void setClassResources(Map<String, String> resources) {
+        this.classResources = resources;
     }
 
-    private Locale getLocale() {
-        return localeResolver.resolveLocale(mc.getHttpServletRequest());
+    public void setEnumResources(Map<? extends Enum<?>, String> enumResources) {
+        this.enumResources = enumResources;
     }
 
-    private String doGetClassResourceName(Class<?> type) {
-        String simpleName = StringUtils.uncapitalize(type.getSimpleName());
-        String thePrefix = locationPrefix == null ? DEFAULT_LOCATION_PREFIX : locationPrefix;
-        String theExtension = resourceExtension == null ? DEFAULT_RESOURCE_EXTENSION : resourceExtension;
-        String viewName = thePrefix + simpleName + theExtension;
-        View view = resolveView(viewName);
-        return view != null ? viewName : null;
+    public void setLocationPrefix(String locationPrefix) {
+        this.locationPrefix = locationPrefix;
     }
 
+    public void setResourceExtension(String resourceExtension) {
+        this.resourceExtension = resourceExtension;
+    }
+    
+    public void setErrorView(String errorView) {
+        this.errorView = errorView;
+    }
+    
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
 
-        if (useClassNames && getViewName(type) != null) {
+        if (useClassNames && getClassResourceName(type) != null) {
             return true;
         }
         if (resourcePath != null || classResourceSupported(type)) {
@@ -181,10 +139,10 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
                 }
             }
         }
-        return mc != null && mc.get(MESSAGE_RESOURCE_PATH_PROPERTY) != null;
+        return getMessageContext() != null && getMessageContext().get(MESSAGE_RESOURCE_PATH_PROPERTY) != null;
     }
 
-    private boolean classResourceSupported(Class<?> type) {
+    protected boolean classResourceSupported(Class<?> type) {
         String typeName = type.getName();
         if (type.isEnum()) {
             for (Object o : enumResources.keySet()) {
@@ -201,71 +159,50 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
         }
         return classResources.containsKey(typeName);
     }
-
-    public void writeTo(Object o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type,
-            MultivaluedMap<String, Object> headers, OutputStream os) throws IOException {
-
-        View view = getView(clazz, o);
-        String attributeName = getBeanName(o);
-        Map<String, Object> model = Collections.singletonMap(attributeName, o);
-
-        try {
-            mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
-            logRedirection(view, attributeName, o);
-            view.render(model, mc.getHttpServletRequest(), mc.getHttpServletResponse());
-        } catch (Throwable ex) {
-            handleViewRenderingException(view, ex);
+    
+    protected String getPathFromMessageContext() {
+        if (getMessageContext() != null) {
+            Object resourcePathProp = getMessageContext().get(MESSAGE_RESOURCE_PATH_PROPERTY);
+            if (resourcePathProp != null) {
+                StringBuilder sb = new StringBuilder();
+                if (locationPrefix != null) {
+                    sb.append(locationPrefix);
+                }
+                sb.append(resourcePathProp.toString());
+                if (resourceExtension != null) {
+                    sb.append(resourceExtension);
+                }
+                return sb.toString();
+            }
         }
+        return null;
     }
-
-    /**
-     * By default we'll try to forward to Spring error handler.
-     *
-     * If no such handler has been set, or if there is an error during error handling,
-     * we throw an error and let CXF handle the internal error.
-     *
-     * @param view view that produced the rendering error
-     * @param exception rendering error
-     */
-    private void handleViewRenderingException(View view, Throwable exception) {
-        LOG.log(Level.WARNING, String.format("Error forwarding to '%s': %s", view, exception.getMessage()), exception);
-        if (errorView != null) {
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_EXCEPTION, exception);
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 500);
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_MESSAGE, exception.getMessage());
-            try {
-                mc.getServletContext().getRequestDispatcher(errorView).forward(mc.getHttpServletRequest(),
-                        mc.getHttpServletResponse());
-            } catch (Exception e) {
-                LOG.log(Level.SEVERE, String.format("Error forwarding to error page '%s': %s",
-                        errorView, e.toString()),
-                        e);
-                handleInternalViewRenderingException(exception);
+    
+    protected String getBeanName(Object bean) {
+        if (beanName != null) {
+            return beanName;
+        }
+        String name = beanNames.get(bean.getClass().getName());
+        if (name != null) {
+            return name;
+        }
+        Class<?> resourceClass = bean.getClass();
+        if (isUseClassNames() && doGetClassResourceName(resourceClass) == null) {
+            for (Class<?> cls : bean.getClass().getInterfaces()) {
+                if (doGetClassResourceName(cls) != null) {
+                    resourceClass = cls;
+                    break;
+                }
             }
-        } else {
-            handleInternalViewRenderingException(exception);
         }
-    }
 
-    private void handleInternalViewRenderingException(Throwable exception) {
-        mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.FALSE);
-        throw ExceptionUtils.toInternalServerErrorException(exception, null);
-    }
-
-    private void logRedirection(View view, String attributeName, Object o) {
-        Level level = logRedirects ? Level.INFO : Level.FINE;
-        if (LOG.isLoggable(level)) {
-            String message = new org.apache.cxf.common.i18n.Message("RESPONSE_REDIRECTED_TO",
-                    BUNDLE, o.getClass().getName(),
-                    attributeName, view).toString();
-            LOG.log(level, message);
-        }
+        return resourceClass.getSimpleName().toLowerCase();
     }
-
-    View getView(Class<?> cls, Object o) {
+    
+    protected String getResourcePath(Class<?> cls, Object o) {
         String currentResourcePath = getPathFromMessageContext();
         if (currentResourcePath != null) {
-            return resolveView(currentResourcePath);
+            return currentResourcePath;
         }
 
         if (!resourcePaths.isEmpty()) {
@@ -273,7 +210,7 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
             String path = getRequestPath();
             for (Map.Entry<String, String> entry : resourcePaths.entrySet()) {
                 if (path.endsWith(entry.getKey())) {
-                    return resolveView(entry.getValue());
+                    return entry.getValue();
                 }
             }
         }
@@ -282,114 +219,104 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
             if (cls.isEnum()) {
                 String enumResource = enumResources.get(o);
                 if (enumResource != null) {
-                    return resolveView(enumResource);
+                    return enumResource;
                 }
                 name += "." + o.toString();
             }
 
             String clsResourcePath = classResources.get(name);
             if (clsResourcePath != null) {
-                return resolveView(clsResourcePath);
+                return clsResourcePath;
             }
         }
 
-        if (useClassNames) {
-            return resolveView(getViewName(cls));
+        if (isUseClassNames()) {
+            return getClassResourceName(cls);
         }
 
-        return resolveView(resourcePath);
+        return resourcePath;
     }
-
-    private View resolveView(String viewName) {
-        try {
-            return viewResolver.resolveViewName(viewName, getLocale());
-        } catch (Exception ex) {
-            LOG.warning(ExceptionUtils.getStackTrace(ex));
-            throw ExceptionUtils.toInternalServerErrorException(ex, null);
-        }
+    
+    protected String getRequestPath() {
+        Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage();
+        return (String)inMessage.get(Message.REQUEST_URI);
     }
-
-    private String getPathFromMessageContext() {
-        if (mc != null) {
-            Object resourcePathProp = mc.get(MESSAGE_RESOURCE_PATH_PROPERTY);
-            if (resourcePathProp != null) {
-                StringBuilder sb = new StringBuilder();
-                if (locationPrefix != null) {
-                    sb.append(locationPrefix);
-                }
-                sb.append(resourcePathProp.toString());
-                if (resourceExtension != null) {
-                    sb.append(resourceExtension);
+    
+    protected String getClassResourceName(Class<?> type) {
+        String resourceName = doGetClassResourceName(type);
+        if (resourceName == null) {
+            for (Class<?> in : type.getInterfaces()) {
+                resourceName = doGetClassResourceName(in);
+                if (resourceName != null) {
+                    break;
                 }
-                return sb.toString();
             }
         }
-        return null;
-    }
-
-    private String getRequestPath() {
-        Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage();
-        return (String) inMessage.get(Message.REQUEST_URI);
-    }
-
-    public void setResourcePath(String resourcePath) {
-        this.resourcePath = resourcePath;
-    }
-
-    public void setBeanNames(Map<String, String> beanNames) {
-        this.beanNames = beanNames;
-    }
-
-    public void setBeanName(String beanName) {
-        this.beanName = beanName;
+        return resourceName;
     }
-
-    public void setLogRedirects(String value) {
-        this.logRedirects = Boolean.valueOf(value);
-    }
-
-    protected String getBeanName(Object bean) {
-        if (beanName != null) {
-            return beanName;
-        }
-        String name = beanNames.get(bean.getClass().getName());
-        if (name != null) {
-            return name;
+    protected String doGetClassResourceName(Class<?> type) {
+        String simpleName = StringUtils.uncapitalize(type.getSimpleName());
+        String thePrefix = locationPrefix == null ? getDefaultLocationPrefix() : locationPrefix;
+        String theExtension = resourceExtension == null ? getDefaultResourceExtension() : resourceExtension;
+        String resourceName = thePrefix + simpleName + theExtension;
+        if (resourceAvailable(resourceName)) {
+            return resourceName;
         }
-        Class<?> resourceClass = bean.getClass();
-        if (useClassNames && doGetClassResourceName(resourceClass) == null) {
-            for (Class<?> cls : bean.getClass().getInterfaces()) {
-                if (doGetClassResourceName(cls) != null) {
-                    resourceClass = cls;
-                    break;
-                }
+        return null;
+    }
+    
+    /**
+     * By default we'll try to forward to the error handler.
+     *
+     * If no such handler has been set, or if there is an error during error handling,
+     * we throw an error and let CXF handle the internal error.
+     *
+     * @param viewName name of the view that produced the rendering error
+     * @param exception rendering error
+     */
+    protected void handleViewRenderingException(String viewName, Throwable exception) {
+        LOG.log(Level.WARNING,
+                String.format("Error forwarding to '%s': %s", viewName, exception.getMessage()), exception);
+        if (errorView != null) {
+            HttpServletRequest httpRequest = getMessageContext().getHttpServletRequest();  
+            httpRequest.setAttribute(RequestDispatcher.ERROR_EXCEPTION, exception);
+            httpRequest.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 500);
+            httpRequest.setAttribute(RequestDispatcher.ERROR_MESSAGE, exception.getMessage());
+            try {
+                getMessageContext().getServletContext().getRequestDispatcher(errorView).forward(
+                    httpRequest,
+                    getMessageContext().getHttpServletResponse());
+            } catch (Exception e) {
+                LOG.log(Level.SEVERE, String.format("Error forwarding to error page '%s': %s",
+                        errorView, e.toString()),
+                        e);
+                handleInternalViewRenderingException(exception);
             }
+        } else {
+            handleInternalViewRenderingException(exception);
         }
-
-        return resourceClass.getSimpleName().toLowerCase();
-    }
-
-    public void setResourcePaths(Map<String, String> resourcePaths) {
-        this.resourcePaths = resourcePaths;
     }
 
-    public void setClassResources(Map<String, String> resources) {
-        this.classResources = resources;
+    protected void handleInternalViewRenderingException(Throwable exception) {
+        getMessageContext().put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.FALSE);
+        throw ExceptionUtils.toInternalServerErrorException(exception, null);
     }
-
-    public void setEnumResources(Map<? extends Enum<?>, String> enumResources) {
-        this.enumResources = enumResources;
+    
+    protected String getDefaultLocationPrefix() {
+        return "";
     }
 
-    public void setLocationPrefix(String locationPrefix) {
-        this.locationPrefix = locationPrefix;
+    protected String getDefaultResourceExtension() {
+        return "";
     }
+    
+    protected abstract boolean resourceAvailable(String resourceName);
 
-    public void setResourceExtension(String resourceExtension) {
-        this.resourceExtension = resourceExtension;
+    public boolean isLogRedirects() {
+        return logRedirects;
     }
 
-    public void setErrorView(String errorView) {
-        this.errorView = errorView;
+    public void setLogRedirects(boolean logRedirects) {
+        this.logRedirects = logRedirects;
     }
-}
\ No newline at end of file
+}
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
index 7624770..a4b3264 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java
@@ -22,7 +22,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -34,29 +33,18 @@ import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriInfo;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
-import org.apache.cxf.message.Message;
-import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 
-@Produces("text/html")
-@Provider
-public class RequestDispatcherProvider extends AbstractConfigurableProvider
-    implements MessageBodyWriter<Object> {
+public class RequestDispatcherProvider extends AbstractResponseViewProvider {
 
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(RequestDispatcherProvider.class);
     private static final Logger LOG = LogUtils.getL7dLogger(RequestDispatcherProvider.class);
@@ -69,119 +57,32 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
     private static final String REQUEST_SCOPE = "request";
     private static final String SESSION_SCOPE = "session";
 
-    private static final String MESSAGE_RESOURCE_PATH_PROPERTY = "redirect.resource.path";
-
     private static final String DEFAULT_RESOURCE_EXTENSION = ".jsp";
     private static final String DEFAULT_LOCATION_PREFIX = "/WEB-INF/";
 
     private String servletContextPath;
-    private String resourcePath;
-    private Map<String, String> resourcePaths = Collections.emptyMap();
-    private Map<String, String> classResources = Collections.emptyMap();
-    private Map<? extends Enum<?>, String> enumResources = Collections.emptyMap();
-    private boolean useClassNames;
-
+    
     private String scope = REQUEST_SCOPE;
-    private Map<String, String> beanNames = Collections.emptyMap();
-    private String beanName;
     private String dispatcherName;
     private String servletPath;
     private boolean useCurrentServlet;
     private boolean saveParametersAsAttributes;
-    private boolean logRedirects;
-    private boolean strictPathCheck;
-    private String locationPrefix;
-    private String resourceExtension;
     private boolean includeResource;
 
-    private MessageContext mc;
-
-    @Context
-    public void setMessageContext(MessageContext context) {
-        this.mc = context;
-    }
-
-    public void setStrictPathCheck(boolean use) {
-        strictPathCheck = use;
-    }
-
-    public void setUseClassNames(boolean use) {
-        useClassNames = use;
-    }
-
-    public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-        return -1;
-    }
-
-    private String getClassResourceName(Class<?> type) {
-        String resourceName = doGetClassResourceName(type);
-        if (resourceName == null) {
-            for (Class<?> in : type.getInterfaces()) {
-                resourceName = doGetClassResourceName(in);
-                if (resourceName != null) {
-                    break;
-                }
-            }
-        }
-        return resourceName;
-    }
-    private String doGetClassResourceName(Class<?> type) {
-        String simpleName = StringUtils.uncapitalize(type.getSimpleName());
-        String thePrefix = locationPrefix == null ? DEFAULT_LOCATION_PREFIX : locationPrefix;
-        String theExtension = resourceExtension == null ? DEFAULT_RESOURCE_EXTENSION : resourceExtension;
-        String resourceName = thePrefix + simpleName + theExtension;
-        if (ResourceUtils.getClasspathResourceURL(resourceName,
-                                                  RequestDispatcherProvider.class,
-                                                  getBus()) != null) {
-            return resourceName;
-        }
-        return null;
-    }
-
-    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-
-        if (useClassNames && getClassResourceName(type) != null) {
-            return true;
-        }
-        if (resourcePath != null || classResourceSupported(type)) {
-            return true;
-        }
-        if (!resourcePaths.isEmpty()) {
-            String path = getRequestPath();
-            for (String requestPath : resourcePaths.keySet()) {
-                boolean result = strictPathCheck ? path.endsWith(requestPath) : path.contains(requestPath);
-                if (result) {
-                    return true;
-                }
-            }
-        }
-        return mc != null && mc.get(MESSAGE_RESOURCE_PATH_PROPERTY) != null;
+    protected String getDefaultLocationPrefix() {
+        return DEFAULT_LOCATION_PREFIX;
     }
-
-    private boolean classResourceSupported(Class<?> type) {
-        String typeName = type.getName();
-        if (type.isEnum()) {
-            for (Object o : enumResources.keySet()) {
-                if (o.getClass().getName().equals(typeName)) {
-                    return true;
-                }
-            }
-            for (String name : classResources.keySet()) {
-                if (name.startsWith(typeName)) {
-                    return true;
-                }
-            }
-            return false;
-        }
-        return classResources.containsKey(typeName);
+    
+    protected String getDefaultResourceExtension() {
+        return DEFAULT_RESOURCE_EXTENSION;
     }
-
+    
     public void writeTo(Object o, Class<?> clazz, Type genericType, Annotation[] annotations,
                         MediaType type, MultivaluedMap<String, Object> headers, OutputStream os)
         throws IOException {
 
         ServletContext sc = getServletContext();
-        HttpServletRequest servletRequest = mc.getHttpServletRequest();
+        HttpServletRequest servletRequest = getMessageContext().getHttpServletRequest();
 
         String path = getResourcePath(clazz, o);
 
@@ -200,7 +101,7 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
 
         try {
             if (!includeResource) {
-                mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
+                getMessageContext().put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
             }
 
             HttpServletRequestFilter requestFilter = new HttpServletRequestFilter(
@@ -214,19 +115,17 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
             setRequestParameters(requestFilter);
             logRedirection(path, attributeName, o);
             if (includeResource) {
-                rd.include(requestFilter, mc.getHttpServletResponse());
+                rd.include(requestFilter, getMessageContext().getHttpServletResponse());
             } else {
-                rd.forward(requestFilter, mc.getHttpServletResponse());
+                rd.forward(requestFilter, getMessageContext().getHttpServletResponse());
             }
         } catch (Throwable ex) {
-            mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.FALSE);
-            LOG.warning(ExceptionUtils.getStackTrace(ex));
-            throw ExceptionUtils.toInternalServerErrorException(ex, null);
+            handleViewRenderingException(theServletPath + path, ex);
         }
     }
 
     private void logRedirection(String path, String attributeName, Object o) {
-        Level level = logRedirects ? Level.INFO : Level.FINE;
+        Level level = isLogRedirects() ? Level.INFO : Level.FINE;
         if (LOG.isLoggable(level)) {
             String message =
                 new org.apache.cxf.common.i18n.Message("RESPONSE_REDIRECTED_TO",
@@ -235,69 +134,8 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         }
     }
 
-    String getResourcePath(Class<?> cls, Object o) {
-        String currentResourcePath = getPathFromMessageContext();
-        if (currentResourcePath != null) {
-            return currentResourcePath;
-        }
-
-        if (!resourcePaths.isEmpty()) {
-
-            String path = getRequestPath();
-            for (Map.Entry<String, String> entry : resourcePaths.entrySet()) {
-                if (path.endsWith(entry.getKey())) {
-                    return entry.getValue();
-                }
-            }
-        }
-        if (!enumResources.isEmpty() || !classResources.isEmpty()) {
-            String name = cls.getName();
-            if (cls.isEnum()) {
-                String enumResource = enumResources.get(o);
-                if (enumResource != null) {
-                    return enumResource;
-                }
-                name += "." + o.toString();
-            }
-
-            String clsResourcePath = classResources.get(name);
-            if (clsResourcePath != null) {
-                return clsResourcePath;
-            }
-        }
-
-        if (useClassNames) {
-            return getClassResourceName(cls);
-        }
-
-        return resourcePath;
-    }
-
-    private String getPathFromMessageContext() {
-        if (mc != null) {
-            Object resourcePathProp = mc.get(MESSAGE_RESOURCE_PATH_PROPERTY);
-            if (resourcePathProp != null) {
-                StringBuilder sb = new StringBuilder();
-                if (locationPrefix != null) {
-                    sb.append(locationPrefix);
-                }
-                sb.append(resourcePathProp.toString());
-                if (resourceExtension != null) {
-                    sb.append(resourceExtension);
-                }
-                return sb.toString();
-            }
-        }
-        return null;
-    }
-
-    private String getRequestPath() {
-        Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage();
-        return (String)inMessage.get(Message.REQUEST_URI);
-    }
-
     protected ServletContext getServletContext() {
-        ServletContext sc = mc.getServletContext();
+        ServletContext sc = getMessageContext().getServletContext();
         if (servletContextPath != null) {
             sc = sc.getContext(servletContextPath);
             if (sc == null) {
@@ -325,10 +163,6 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         return rd;
     }
 
-    public void setResourcePath(String resourcePath) {
-        this.resourcePath = resourcePath;
-    }
-
     public void setServletContextPath(String servletContextPath) {
         this.servletContextPath = servletContextPath;
     }
@@ -337,42 +171,9 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         this.scope = scope;
     }
 
-    public void setBeanNames(Map<String, String> beanNames) {
-        this.beanNames = beanNames;
-    }
-
-    public void setBeanName(String beanName) {
-        this.beanName = beanName;
-    }
-
-    public void setLogRedirects(String value) {
-        this.logRedirects = Boolean.valueOf(value);
-    }
-
-    protected String getBeanName(Object bean) {
-        if (beanName != null) {
-            return beanName;
-        }
-        String name = beanNames.get(bean.getClass().getName());
-        if (name != null) {
-            return name;
-        }
-        Class<?> resourceClass = bean.getClass();
-        if (useClassNames && doGetClassResourceName(resourceClass) == null) {
-            for (Class<?> cls : bean.getClass().getInterfaces()) {
-                if (doGetClassResourceName(cls) != null) {
-                    resourceClass = cls;
-                    break;
-                }
-            }
-        }
-
-        return resourceClass.getSimpleName().toLowerCase();
-    }
-
     protected void setRequestParameters(HttpServletRequestFilter request) {
-        if (mc != null) {
-            UriInfo ui = mc.getUriInfo();
+        if (getMessageContext() != null) {
+            UriInfo ui = getMessageContext().getUriInfo();
             MultivaluedMap<String, String> params = ui.getPathParameters();
             for (Map.Entry<String, List<String>> entry : params.entrySet()) {
                 String value = entry.getValue().get(0);
@@ -391,7 +192,7 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
             request.setParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
             request.setParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
             request.setParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
-            request.setParameter(WEBAPP_BASE_PATH_PARAMETER, (String)mc.get("http.base.path"));
+            request.setParameter(WEBAPP_BASE_PATH_PARAMETER, (String)getMessageContext().get("http.base.path"));
         }
     }
 
@@ -410,22 +211,10 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         this.servletPath = path;
     }
 
-    public void setResourcePaths(Map<String, String> resourcePaths) {
-        this.resourcePaths = resourcePaths;
-    }
-
-    public void setClassResources(Map<String, String> resources) {
-        this.classResources = resources;
-    }
-
     public void setSaveParametersAsAttributes(boolean saveParametersAsAttributes) {
         this.saveParametersAsAttributes = saveParametersAsAttributes;
     }
 
-    public void setEnumResources(Map<? extends Enum<?>, String> enumResources) {
-        this.enumResources = enumResources;
-    }
-
     public void setUseCurrentServlet(boolean useCurrentServlet) {
         this.useCurrentServlet = useCurrentServlet;
     }
@@ -434,14 +223,6 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         this.includeResource = includeResource;
     }
 
-    public void setLocationPrefix(String locationPrefix) {
-        this.locationPrefix = locationPrefix;
-    }
-
-    public void setResourceExtension(String resourceExtension) {
-        this.resourceExtension = resourceExtension;
-    }
-
     protected static class HttpServletRequestFilter extends HttpServletRequestWrapper {
 
         private Map<String, String[]> params;
@@ -501,4 +282,11 @@ public class RequestDispatcherProvider extends AbstractConfigurableProvider
         }
 
     }
+
+    @Override
+    protected boolean resourceAvailable(String resourceName) {
+        return ResourceUtils.getClasspathResourceURL(resourceName,
+                                              RequestDispatcherProvider.class,
+                                              getBus()) != null;
+    }
 }
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java
index 8fa38cd..8b89ed4 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/SpringViewResolverProvider.java
@@ -29,22 +29,13 @@ import java.util.ResourceBundle;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.servlet.RequestDispatcher;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
 
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
-import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.jaxrs.ext.MessageContext;
-import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
+import org.apache.cxf.jaxrs.provider.AbstractResponseViewProvider;
 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
-import org.apache.cxf.message.Message;
-import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.springframework.web.servlet.LocaleResolver;
 import org.springframework.web.servlet.View;
@@ -66,50 +57,16 @@ import org.springframework.web.servlet.ViewResolver;
  }
  * </pre>
  */
-@Produces("text/html")
-@Provider
-public class SpringViewResolverProvider extends AbstractConfigurableProvider implements MessageBodyWriter<Object> {
+public class SpringViewResolverProvider extends AbstractResponseViewProvider {
 
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(SpringViewResolverProvider.class);
 
     private static final Logger LOG = LogUtils.getL7dLogger(SpringViewResolverProvider.class);
 
-    private static final String MESSAGE_RESOURCE_PATH_PROPERTY = "redirect.resource.path";
-
-    private static final String DEFAULT_RESOURCE_EXTENSION = "";
-
-    private static final String DEFAULT_LOCATION_PREFIX = "";
-
     private final ViewResolver viewResolver;
 
-    private String resourcePath;
-
-    private Map<String, String> resourcePaths = Collections.emptyMap();
-
-    private Map<String, String> classResources = Collections.emptyMap();
-
-    private Map<? extends Enum<?>, String> enumResources = Collections.emptyMap();
-
-    private boolean useClassNames;
-
-    private Map<String, String> beanNames = Collections.emptyMap();
-
-    private String beanName;
-
-    private boolean logRedirects;
-
-    private boolean strictPathCheck;
-
-    private String locationPrefix;
-
-    private String resourceExtension;
-
-    private MessageContext mc;
-
     private LocaleResolver localeResolver;
 
-    private String errorView = "/error";
-
     public SpringViewResolverProvider(ViewResolver viewResolver, LocaleResolver localeResolver) {
         if (viewResolver == null) {
             throw new IllegalArgumentException("Argument viewResolver is required");
@@ -121,85 +78,9 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
         this.localeResolver = localeResolver;
     }
 
-    @Context
-    public void setMessageContext(MessageContext context) {
-        this.mc = context;
-    }
-
-    public void setStrictPathCheck(boolean use) {
-        strictPathCheck = use;
-    }
-
-    public void setUseClassNames(boolean use) {
-        useClassNames = use;
-    }
-
-    public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-        return -1;
-    }
-
-    private String getViewName(Class<?> type) {
-        String viewName = doGetClassResourceName(type);
-        if (viewName == null) {
-            for (Class<?> in : type.getInterfaces()) {
-                viewName = doGetClassResourceName(in);
-                if (viewName != null) {
-                    break;
-                }
-            }
-        }
-        return viewName;
-    }
-
+    
     private Locale getLocale() {
-        return localeResolver.resolveLocale(mc.getHttpServletRequest());
-    }
-
-    private String doGetClassResourceName(Class<?> type) {
-        String simpleName = StringUtils.uncapitalize(type.getSimpleName());
-        String thePrefix = locationPrefix == null ? DEFAULT_LOCATION_PREFIX : locationPrefix;
-        String theExtension = resourceExtension == null ? DEFAULT_RESOURCE_EXTENSION : resourceExtension;
-        String viewName = thePrefix + simpleName + theExtension;
-        View view = resolveView(viewName);
-        return view != null ? viewName : null;
-    }
-
-    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-
-        if (useClassNames && getViewName(type) != null) {
-            return true;
-        }
-        if (resourcePath != null || classResourceSupported(type)) {
-            return true;
-        }
-        if (!resourcePaths.isEmpty()) {
-            String path = getRequestPath();
-            for (String requestPath : resourcePaths.keySet()) {
-                boolean result = strictPathCheck ? path.endsWith(requestPath) : path.contains(requestPath);
-                if (result) {
-                    return true;
-                }
-            }
-        }
-        return mc != null && mc.get(MESSAGE_RESOURCE_PATH_PROPERTY) != null;
-    }
-
-    private boolean classResourceSupported(Class<?> type) {
-        String typeName = type.getName();
-        if (type.isEnum()) {
-            for (Object o : enumResources.keySet()) {
-                if (o.getClass().getName().equals(typeName)) {
-                    return true;
-                }
-            }
-            for (String name : classResources.keySet()) {
-                if (name.startsWith(typeName)) {
-                    return true;
-                }
-            }
-            return false;
-        }
-        return classResources.containsKey(typeName);
+        return localeResolver.resolveLocale(getMessageContext().getHttpServletRequest());
     }
 
     public void writeTo(Object o, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType type,
@@ -210,50 +91,18 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
         Map<String, Object> model = Collections.singletonMap(attributeName, o);
 
         try {
-            mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
+            getMessageContext().put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.TRUE);
             logRedirection(view, attributeName, o);
-            view.render(model, mc.getHttpServletRequest(), mc.getHttpServletResponse());
+            view.render(model, getMessageContext().getHttpServletRequest(),
+                        getMessageContext().getHttpServletResponse());
         } catch (Throwable ex) {
-            handleViewRenderingException(view, ex);
-        }
-    }
-
-    /**
-     * By default we'll try to forward to Spring error handler.
-     *
-     * If no such handler has been set, or if there is an error during error handling,
-     * we throw an error and let CXF handle the internal error.
-     *
-     * @param view view that produced the rendering error
-     * @param exception rendering error
-     */
-    private void handleViewRenderingException(View view, Throwable exception) {
-        LOG.log(Level.WARNING, String.format("Error forwarding to '%s': %s", view, exception.getMessage()), exception);
-        if (errorView != null) {
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_EXCEPTION, exception);
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 500);
-            mc.getHttpServletRequest().setAttribute(RequestDispatcher.ERROR_MESSAGE, exception.getMessage());
-            try {
-                mc.getServletContext().getRequestDispatcher(errorView).forward(mc.getHttpServletRequest(),
-                        mc.getHttpServletResponse());
-            } catch (Exception e) {
-                LOG.log(Level.SEVERE, String.format("Error forwarding to error page '%s': %s",
-                        errorView, e.toString()),
-                        e);
-                handleInternalViewRenderingException(exception);
-            }
-        } else {
-            handleInternalViewRenderingException(exception);
+            handleViewRenderingException(view.toString(), ex);
         }
     }
-
-    private void handleInternalViewRenderingException(Throwable exception) {
-        mc.put(AbstractHTTPDestination.REQUEST_REDIRECTED, Boolean.FALSE);
-        throw ExceptionUtils.toInternalServerErrorException(exception, null);
-    }
+    
 
     private void logRedirection(View view, String attributeName, Object o) {
-        Level level = logRedirects ? Level.INFO : Level.FINE;
+        Level level = isLogRedirects() ? Level.INFO : Level.FINE;
         if (LOG.isLoggable(level)) {
             String message = new org.apache.cxf.common.i18n.Message("RESPONSE_REDIRECTED_TO",
                     BUNDLE, o.getClass().getName(),
@@ -263,41 +112,12 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
     }
 
     View getView(Class<?> cls, Object o) {
-        String currentResourcePath = getPathFromMessageContext();
-        if (currentResourcePath != null) {
-            return resolveView(currentResourcePath);
-        }
-
-        if (!resourcePaths.isEmpty()) {
-
-            String path = getRequestPath();
-            for (Map.Entry<String, String> entry : resourcePaths.entrySet()) {
-                if (path.endsWith(entry.getKey())) {
-                    return resolveView(entry.getValue());
-                }
-            }
-        }
-        if (!enumResources.isEmpty() || !classResources.isEmpty()) {
-            String name = cls.getName();
-            if (cls.isEnum()) {
-                String enumResource = enumResources.get(o);
-                if (enumResource != null) {
-                    return resolveView(enumResource);
-                }
-                name += "." + o.toString();
-            }
-
-            String clsResourcePath = classResources.get(name);
-            if (clsResourcePath != null) {
-                return resolveView(clsResourcePath);
-            }
-        }
-
-        if (useClassNames) {
-            return resolveView(getViewName(cls));
+        String path = getResourcePath(cls, o);
+        if (path != null) {
+            return resolveView(path);
+        } else {
+            return null;
         }
-
-        return resolveView(resourcePath);
     }
 
     private View resolveView(String viewName) {
@@ -309,87 +129,8 @@ public class SpringViewResolverProvider extends AbstractConfigurableProvider imp
         }
     }
 
-    private String getPathFromMessageContext() {
-        if (mc != null) {
-            Object resourcePathProp = mc.get(MESSAGE_RESOURCE_PATH_PROPERTY);
-            if (resourcePathProp != null) {
-                StringBuilder sb = new StringBuilder();
-                if (locationPrefix != null) {
-                    sb.append(locationPrefix);
-                }
-                sb.append(resourcePathProp.toString());
-                if (resourceExtension != null) {
-                    sb.append(resourceExtension);
-                }
-                return sb.toString();
-            }
-        }
-        return null;
-    }
-
-    private String getRequestPath() {
-        Message inMessage = PhaseInterceptorChain.getCurrentMessage().getExchange().getInMessage();
-        return (String) inMessage.get(Message.REQUEST_URI);
-    }
-
-    public void setResourcePath(String resourcePath) {
-        this.resourcePath = resourcePath;
-    }
-
-    public void setBeanNames(Map<String, String> beanNames) {
-        this.beanNames = beanNames;
-    }
-
-    public void setBeanName(String beanName) {
-        this.beanName = beanName;
-    }
-
-    public void setLogRedirects(String value) {
-        this.logRedirects = Boolean.valueOf(value);
-    }
-
-    protected String getBeanName(Object bean) {
-        if (beanName != null) {
-            return beanName;
-        }
-        String name = beanNames.get(bean.getClass().getName());
-        if (name != null) {
-            return name;
-        }
-        Class<?> resourceClass = bean.getClass();
-        if (useClassNames && doGetClassResourceName(resourceClass) == null) {
-            for (Class<?> cls : bean.getClass().getInterfaces()) {
-                if (doGetClassResourceName(cls) != null) {
-                    resourceClass = cls;
-                    break;
-                }
-            }
-        }
-
-        return resourceClass.getSimpleName().toLowerCase();
-    }
-
-    public void setResourcePaths(Map<String, String> resourcePaths) {
-        this.resourcePaths = resourcePaths;
-    }
-
-    public void setClassResources(Map<String, String> resources) {
-        this.classResources = resources;
-    }
-
-    public void setEnumResources(Map<? extends Enum<?>, String> enumResources) {
-        this.enumResources = enumResources;
-    }
-
-    public void setLocationPrefix(String locationPrefix) {
-        this.locationPrefix = locationPrefix;
-    }
-
-    public void setResourceExtension(String resourceExtension) {
-        this.resourceExtension = resourceExtension;
-    }
-
-    public void setErrorView(String errorView) {
-        this.errorView = errorView;
+    @Override
+    protected boolean resourceAvailable(String resourceName) {
+        return resolveView(resourceName) != null;
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
['"commits@cxf.apache.org" <commits@cxf.apache.org>'].

Mime
View raw message