Author: ate
Date: Tue Sep 4 15:53:32 2012
New Revision: 1380699
URL: http://svn.apache.org/viewvc?rev=1380699&view=rev
Log:
RAVE-694: adding HmvcConfig method arguments support to inject Hmvc controller
configuration into controller methods at runtime
As example adding a default/builtin ViewOnlyController which just takes and
returns the externally configured viewName to render, so no need to code/define
controllers for this anymore.
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
(with props)
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
(with props)
Modified:
rave/sandbox/content-services/demo-portal/src/main/resources/page-configuration.xml
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerConfiguration.java
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethod.java
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodHandlerAdapter.java
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodMappingByConfig.java
Modified:
rave/sandbox/content-services/demo-portal/src/main/resources/page-configuration.xml
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/demo-portal/src/main/resources/page-configuration.xml?rev=1380699&r1=1380698&r2=1380699&view=diff
==============================================================================
---
rave/sandbox/content-services/demo-portal/src/main/resources/page-configuration.xml
(original)
+++
rave/sandbox/content-services/demo-portal/src/main/resources/page-configuration.xml
Tue Sep 4 15:53:32 2012
@@ -22,7 +22,7 @@
<page-definition name="myPage3" description="My Page" displayname="My page"
controller="org.apache.rave.portal.web.hmvc.example.MyPageController">
<page-fragment name="head"
controller="org.apache.rave.portal.web.hmvc.example.MyHeaderController"/>
<page-fragment name="body"
controller="org.apache.rave.portal.web.hmvc.example.MyContentController"/>
- <page-fragment name="footer"
controller="org.apache.rave.portal.web.hmvc.example.MyFooterController"/>
+ <page-fragment name="footer" viewname="hmvc/footer"/>
</page-definition>
<page-definition name="userPage"
controller="org.apache.rave.portal.web.hmvc.controller.UserPageController">
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java?rev=1380699&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
Tue Sep 4 15:53:32 2012
@@ -0,0 +1,22 @@
+/*
+ * 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.rave.portal.web.hmvc;
+
+public interface HmvcConfig {
+ String getName();
+ String getViewName();
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfig.java
------------------------------------------------------------------------------
svn:keywords = Id
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java?rev=1380699&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
Tue Sep 4 15:53:32 2012
@@ -0,0 +1,37 @@
+/*
+ * 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.rave.portal.web.hmvc;
+
+import org.springframework.core.MethodParameter;
+import org.springframework.web.bind.support.WebDataBinderFactory;
+import org.springframework.web.context.request.NativeWebRequest;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.method.support.HandlerMethodArgumentResolver;
+import org.springframework.web.method.support.ModelAndViewContainer;
+
+public class HmvcConfigMethodArgumentResolver implements
HandlerMethodArgumentResolver {
+
+ public boolean supportsParameter(final MethodParameter parameter) {
+ return parameter.getParameterType().isAssignableFrom(HmvcConfig.class);
+ }
+
+ public Object resolveArgument(
+ MethodParameter parameter, ModelAndViewContainer mavContainer,
+ NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
+ return
(HmvcConfig)webRequest.getAttribute(HmvcHandlerMethod.HMVC_CONFIG,
RequestAttributes.SCOPE_REQUEST);
+ }
+}
\ No newline at end of file
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcConfigMethodArgumentResolver.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerConfiguration.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerConfiguration.java?rev=1380699&r1=1380698&r2=1380699&view=diff
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerConfiguration.java
(original)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerConfiguration.java
Tue Sep 4 15:53:32 2012
@@ -22,7 +22,7 @@ import java.util.Map;
import org.springframework.web.method.HandlerMethod;
-public class HmvcHandlerConfiguration {
+public class HmvcHandlerConfiguration implements HmvcConfig {
private String name;
private String targetId;
private HmvcHandlerConfiguration parent;
Modified:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethod.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethod.java?rev=1380699&r1=1380698&r2=1380699&view=diff
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethod.java
(original)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethod.java
Tue Sep 4 15:53:32 2012
@@ -28,6 +28,7 @@ public class HmvcHandlerMethod extends H
public static final String REQUEST_MAPPING_INFO =
HmvcHandlerMethod.class.getName() + ".RequestMappingInfo";
public static final String VIEW_RESPONSE_STATE =
HmvcHandlerMethod.class.getName() + ".ViewResponseState";
public static final String MODEL_AND_VIEW =
HmvcHandlerMethod.class.getName() + ".ModelAndView";
+ public static final String HMVC_CONFIG = HmvcHandlerMethod.class.getName()
+ ".HmvcConfig";
private HmvcHandlerConfiguration configuration;
private BeanFactory beanFactory;
Modified:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodHandlerAdapter.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodHandlerAdapter.java?rev=1380699&r1=1380698&r2=1380699&view=diff
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodHandlerAdapter.java
(original)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodHandlerAdapter.java
Tue Sep 4 15:53:32 2012
@@ -16,13 +16,16 @@
*/
package org.apache.rave.portal.web.hmvc;
+import java.util.ArrayList;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.method.annotation.ForkedRequestMappingHandlerAdapter;
@@ -30,12 +33,27 @@ import org.springframework.web.servlet.m
public class HmvcHandlerMethodHandlerAdapter extends
ForkedRequestMappingHandlerAdapter {
@Override
+ public void afterPropertiesSet() {
+ if (getArgumentResolvers() == null && getCustomArgumentResolvers() ==
null) {
+ List<HandlerMethodArgumentResolver> customResolvers = new
ArrayList<HandlerMethodArgumentResolver>();
+ customResolvers.add(new HmvcConfigMethodArgumentResolver());
+ setCustomArgumentResolvers(customResolvers);
+ }
+ super.afterPropertiesSet();
+ }
+
+ @Override
protected ModelAndView handleInternal(HttpServletRequest request,
HttpServletResponse response,
HandlerMethod handlerMethod) throws
Exception {
+ HmvcHandlerMethod hm = (handlerMethod instanceof HmvcHandlerMethod) ?
(HmvcHandlerMethod)handlerMethod : null;
+ if (hm != null) {
+ request.setAttribute(HmvcHandlerMethod.HMVC_CONFIG,
hm.getConfiguration());
+ }
ModelAndView mv = super.handleInternal(request, response,
handlerMethod);
- if (handlerMethod instanceof HmvcHandlerMethod) {
- HmvcHandlerMethod hm = (HmvcHandlerMethod) handlerMethod;
+ if (hm != null) {
+
+ request.removeAttribute(HmvcHandlerMethod.HMVC_CONFIG);
Map<String, ModelAndView> cmvMap = new LinkedHashMap<String,
ModelAndView>();
for (String name : hm.getConfiguration().getChildren().keySet()) {
Modified:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodMappingByConfig.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodMappingByConfig.java?rev=1380699&r1=1380698&r2=1380699&view=diff
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodMappingByConfig.java
(original)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/HmvcHandlerMethodMappingByConfig.java
Tue Sep 4 15:53:32 2012
@@ -22,7 +22,6 @@ package org.apache.rave.portal.web.hmvc;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -46,6 +45,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -85,8 +86,9 @@ public class HmvcHandlerMethodMappingByC
private boolean configured;
- // temporary hacked open *reference* to the real but private
handlerMethods map in AbstractHandlerMethodMapping.java
+ // temporary hacked open *references* to the real but private
handlerMethods and urlMap maps in AbstractHandlerMethodMapping.java
private Map<RequestMappingInfo, HandlerMethod> handlerMethods;
+ private MultiValueMap<String, RequestMappingInfo> urlMap = new
LinkedMultiValueMap<String, RequestMappingInfo>();
/**
* Used only when JCR config manager is used
@@ -95,13 +97,16 @@ public class HmvcHandlerMethodMappingByC
public HmvcHandlerMethodMappingByConfig() {
super();
- // temporary dirty hack to access private handlerMethods map in
AbstractHandlerMethodMapping.java
- // see #getHandlerMethodsInternal() method which should be provided on
that class
+ // temporary dirty hack to access private handlerMethods and urlMap
maps in AbstractHandlerMethodMapping.java
+ // see #getHandlerMethodsInternal() and #getUrlMapInternal() methods
which should be provided on that class
try {
- Field f =
AbstractHandlerMethodMapping.class.getDeclaredField("handlerMethods");
- f.setAccessible(true);
+ Field f1 =
AbstractHandlerMethodMapping.class.getDeclaredField("handlerMethods");
+ f1.setAccessible(true);
+ Field f2 =
AbstractHandlerMethodMapping.class.getDeclaredField("urlMap");
+ f2.setAccessible(true);
try {
- handlerMethods = (Map<RequestMappingInfo,
HandlerMethod>)f.get(this);
+ handlerMethods = (Map<RequestMappingInfo,
HandlerMethod>)f1.get(this);
+ urlMap = (MultiValueMap<String,
RequestMappingInfo>)f2.get(this);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unexpected", e);
}
@@ -119,6 +124,14 @@ public class HmvcHandlerMethodMappingByC
return this.handlerMethods;
}
+ /**
+ * Temporary method which should be made available on super class
AbstractHandlerMethodMapping.java instead
+ * to allow access to the internal private urlMap map.
+ */
+ protected MultiValueMap<String, RequestMappingInfo> getUrlMapInternal() {
+ return this.urlMap;
+ }
+
@Override
protected void initHandlerMethods() {
// TODO: figure out better way to inject JcrConfigManager
@@ -246,6 +259,7 @@ public class HmvcHandlerMethodMappingByC
// reset
getHandlerMethodsInternal().clear();
+ getUrlMapInternal().clear();
for (UrlMapping urlMapping : configuration.getMappings()) {
PageDefinition pageDefinition = urlMapping.getPageDefinition();
@@ -312,6 +326,10 @@ public class HmvcHandlerMethodMappingByC
// UTILITIES
//*************************************************************************************
protected Class<?> getControllerClass(String controller) {
+ if (controller == null) {
+ // TODO: temporarily direct injection of ViewOnlyController,
should become pluggable/configurable
+ controller = ViewOnlyController.class.getName();
+ }
Class<?> userType = classFromString(controller);
return userType != null ? ClassUtils.getUserClass(userType) : null;
}
Added:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
URL:
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java?rev=1380699&view=auto
==============================================================================
---
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
(added)
+++
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
Tue Sep 4 15:53:32 2012
@@ -0,0 +1,29 @@
+/*
+ * 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.rave.portal.web.hmvc;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@Controller
+public class ViewOnlyController {
+
+ @Hmvc @RequestMapping
+ public String render(HmvcConfig config) {
+ return config.getViewName();
+ }
+}
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/hmvc/ViewOnlyController.java
------------------------------------------------------------------------------
svn:keywords = Id