Revision: 7dd38c5ac05f
Branch: default
Author: lucio.benfante <
lucio.b...@gmail.com>
Date: Thu Sep 20 10:12:43 2012
Log: Enhanced the mark position mechanism.
http://code.google.com/p/parancoe/source/detail?r=7dd38c5ac05f
Added:
/parancoe-web/src/main/java/org/parancoe/web/MarkPositionInterceptor.java
/parancoe-web/src/main/java/org/parancoe/web/test/TagTest.java
/parancoe-web/src/main/java/org/parancoe/web/util/MarkPositionHelper.java
/parancoe-web/src/test/java/org/parancoe/web/tag/MarkPositionTagTest.java
/parancoe-web/src/test/java/org/parancoe/web/tag/MarkedPositionTagTest.java
Modified:
/parancoe-web/src/main/java/org/parancoe/web/tag/MarkPositionTag.java
/parancoe-web/src/main/java/org/parancoe/web/tag/MarkedPositionTag.java
/parancoe-web/src/main/resources/META-INF/parancoe.tld
=======================================
--- /dev/null
+++
/parancoe-web/src/main/java/org/parancoe/web/MarkPositionInterceptor.java
Thu Sep 20 10:12:43 2012
@@ -0,0 +1,47 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <
in...@parancoe.org>
+ *
+ * This file is part of Parancoe Web.
+ *
+ * Licensed 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.parancoe.web;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.parancoe.web.util.MarkPositionHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+/**
+ *
+ * @author Lucio Benfante <
lu...@benfante.com>
+ */
+public class MarkPositionInterceptor extends HandlerInterceptorAdapter {
+
+ private static final Logger log =
LoggerFactory.getLogger(MarkPositionInterceptor.class);
+
+ @Override
+ public void postHandle(HttpServletRequest request, HttpServletResponse
response, Object handler,
+ ModelAndView modelAndView) throws Exception {
+ log.debug("Executing postHandle.");
+ String backUrl = request.getParameter("_back");
+ if (backUrl != null) {
+ String pathId = request.getParameter("_pathId");
+ MarkPositionHelper.markPosition(request, pathId, backUrl);
+ log.debug("Marked position ({}) for pathId ({}).", new
Object[]{backUrl, pathId});
+ }
+ }
+}
=======================================
--- /dev/null
+++ /parancoe-web/src/main/java/org/parancoe/web/test/TagTest.java Thu Sep
20 10:12:43 2012
@@ -0,0 +1,116 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <
in...@parancoe.org>
+ *
+ * This file is part of Parancoe Web.
+ *
+ * Licensed 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.parancoe.web.test;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.core.io.FileSystemResourceLoader;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.mock.web.MockMultipartHttpServletRequest;
+import org.springframework.mock.web.MockPageContext;
+import org.springframework.mock.web.MockServletContext;
+import org.springframework.web.context.ContextLoader;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.XmlWebApplicationContext;
+import org.springframework.web.servlet.support.JspAwareRequestContext;
+import org.springframework.web.servlet.support.RequestContext;
+import org.springframework.web.servlet.tags.RequestContextAwareTag;
+import org.springframework.web.util.WebUtils;
+
+public abstract class TagTest extends BaseTest {
+
+ protected MockMultipartHttpServletRequest mpReq;
+ protected MockHttpServletRequest req;
+ protected MockHttpServletResponse res;
+ protected MockPageContext pc;
+ protected RequestContext rc;
+
+ @Override
+ public void onSetUpBeforeTransaction() throws Exception {
+ super.onSetUpBeforeTransaction();
+ resetRequestAndResponse();
+ }
+
+ @Override
+ public void onTearDownAfterTransaction() throws Exception {
+ super.onTearDownAfterTransaction();
+ mpReq = null;
+ req = null;
+ res = null;
+ pc = null;
+ }
+
+ /**
+ * Reset the request and the response, maintaining the same session.
Useful, for example, to
+ * call a post after calling the get of the form.
+ */
+ protected void resetRequestAndResponse() {
+ HttpSession httpSession = null;
+ // preparing the multipart request
+ if (mpReq != null) {
+ httpSession = mpReq.getSession();
+ }
+ mpReq = new MockMultipartHttpServletRequest();
+ mpReq.setSession(httpSession);
+ mpReq.setMethod("GET");
+ // preparing the normal request
+ if (req != null) {
+ httpSession = req.getSession();
+ }
+ req = new MockHttpServletRequest();
+ req.setSession(httpSession);
+ req.setMethod("GET");
+ req.setContextPath("/testctx");
+ req.setRequestURI("/test/request/uri");
+
req.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/test/forward/request/uri");
+ req.setQueryString("p1=v1&p2=v2&p3=v3");
+ res = new MockHttpServletResponse();
+ pc = new MockPageContext(((WebApplicationContext)
this.getApplicationContext()).
+ getServletContext(), req, res);
+ rc = new JspAwareRequestContext(pc);
+
pc.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, rc);
+ }
+
+ @Override
+ protected ConfigurableApplicationContext createApplicationContext(
+ String[] locations) {
+ FileSystemResourceLoader rl = new FileSystemResourceLoader();
+ MockServletContext servletContext = new MockServletContext(rl);
+ servletContext.setMinorVersion(4);
+ servletContext.registerContext("/test", servletContext);
+ servletContext.setServletContextName("/test");
+
servletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
+ arrayToString(locations));
+ ContextLoader loader = new ContextLoader();
+ WebApplicationContext context =
loader.initWebApplicationContext(servletContext);
+ return (ConfigurableApplicationContext) context;
+ }
+
+ private String arrayToString(String[] locations) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < locations.length - 1; i++) {
+ sb.append(locations[i]).append(',');
+ }
+ if (locations.length > 0) {
+ sb.append(locations[locations.length - 1]);
+ }
+ return sb.toString();
+ }
+}
=======================================
--- /dev/null
+++
/parancoe-web/src/main/java/org/parancoe/web/util/MarkPositionHelper.java
Thu Sep 20 10:12:43 2012
@@ -0,0 +1,70 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <
in...@parancoe.org>
+ *
+ * This file is part of Parancoe Web.
+ *
+ * Licensed 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.parancoe.web.util;
+
+import java.util.Enumeration;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import org.apache.commons.lang.StringUtils;
+import org.parancoe.web.MarkPositionInterceptor;
+import org.parancoe.web.tag.MarkPositionTag;
+import org.parancoe.web.tag.MarkedPositionTag;
+
+/**
+ * Utility methods for marking positions during navigations.
+ *
+ * @see MarkPositionInterceptor
+ * @see MarkPositionTag
+ * @see MarkedPositionTag
+ *
+ * @author Lucio Benfante <
lu...@benfante.com>
+ */
+public class MarkPositionHelper {
+
+ public static String getMarkedPosition(HttpSession session, String
pathId) {
+ String backUrl = (String)
session.getAttribute(getSessionAttributeKey(pathId));
+ return StringUtils.isNotBlank(backUrl) ? backUrl : "";
+ }
+
+ public static String getSessionAttributeKey(String pathId) {
+ return MarkPositionTag.PREFIX + pathId;
+ }
+
+ public static void markPosition(HttpServletRequest request, String
pathId, String url) {
+
request.getSession().setAttribute(MarkPositionHelper.getSessionAttributeKey(pathId),
url);
+ }
+
+ public static void markPosition(HttpSession session, String pathId,
String url) {
+
session.setAttribute(MarkPositionHelper.getSessionAttributeKey(pathId),
url);
+ }
+
+ public static void unmarkPosition(HttpSession session, String pathId) {
+
session.removeAttribute(MarkPositionHelper.getSessionAttributeKey(pathId));
+ }
+
+ public static void unmarkAllPositions(HttpSession session) {
+ Enumeration attributeNames = session.getAttributeNames();
+ while (attributeNames.hasMoreElements()) {
+ String name = (String)attributeNames.nextElement();
+ if (name.startsWith(MarkPositionTag.PREFIX)) {
+ session.removeAttribute(name);
+ }
+ }
+ }
+
+}
=======================================
--- /dev/null
+++
/parancoe-web/src/test/java/org/parancoe/web/tag/MarkPositionTagTest.java
Thu Sep 20 10:12:43 2012
@@ -0,0 +1,70 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <
in...@parancoe.org>
+ *
+ * This file is part of Parancoe Web.
+ *
+ * Licensed 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.parancoe.web.tag;
+
+import javax.servlet.jsp.tagext.Tag;
+import org.parancoe.web.test.TagTest;
+import org.parancoe.web.util.MarkPositionHelper;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+/**
+ *
+ * @author Lucio Benfante <
lu...@benfante.com>
+ */
+public class MarkPositionTagTest extends TagTest {
+
+ public void testDoStartTag() throws Exception {
+ MarkPositionTag tag = new MarkPositionTag();
+ tag.setPageContext(this.pc);
+ String pathId = "testPathId";
+ tag.setPathId(pathId);
+ int result = tag.doStartTag();
+ assertEquals(Tag.SKIP_BODY, result);
+ String output =
((MockHttpServletResponse)this.pc.getResponse()).getContentAsString();
+ assertEquals("", output);
+ assertEquals("/test/forward/request/uri?p1=v1&p2=v2&p3=v3",
this.req.getSession().getAttribute(MarkPositionHelper.getSessionAttributeKey(pathId)));
+ }
+
+ public void testDoStartTagWithAbsolute() throws Exception {
+ MarkPositionTag tag = new MarkPositionTag();
+ tag.setPageContext(this.pc);
+ tag.setUseFullUri(true);
+ String pathId = "testPathId";
+ tag.setPathId(pathId);
+ int result = tag.doStartTag();
+ assertEquals(Tag.SKIP_BODY, result);
+ String output =
((MockHttpServletResponse)this.pc.getResponse()).getContentAsString();
+ assertEquals("", output);
+
assertEquals("
http://localhost/testctx/test/forward/request/uri?p1=v1&p2=v2&p3=v3",
this.req.getSession().getAttribute(MarkPositionHelper.getSessionAttributeKey(pathId)));
+ }
+
+ @Override
+ public Class[] getFixtureClasses() {
+ return new Class[]{};
+ }
+
+ @Override
+ protected String[] getConfigLocations() {
+ return new String[]{
+ "classpath:org/lambico/spring/dao/hibernate/genericDao.xml",
+ "classpath:org/lambico/spring/dao/hibernate/applicationContextBase.xml",
+ "classpath:org/parancoe/web/parancoeBase.xml",
+ "classpath:spring-test.xml"};
+ }
+
+}
=======================================
--- /dev/null
+++
/parancoe-web/src/test/java/org/parancoe/web/tag/MarkedPositionTagTest.java
Thu Sep 20 10:12:43 2012
@@ -0,0 +1,70 @@
+/**
+ * Copyright (C) 2006-2010 The Parancoe Team <
in...@parancoe.org>
+ *
+ * This file is part of Parancoe Web.
+ *
+ * Licensed 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.parancoe.web.tag;
+
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.Tag;
+import org.junit.Test;
+import org.parancoe.web.test.TagTest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+/**
+ *
+ * @author Lucio Benfante <
lu...@benfante.com>
+ */
+public class MarkedPositionTagTest extends TagTest {
+
+ /**
+ * Test of doStartTagInternal method, of class MarkedPositionTag.
+ */
+ @Test
+ public void testDoStartTagInternal() throws Exception {
+ String pathId = "testPathId";
+ markPosition(pathId);
+ resetRequestAndResponse();
+ MarkedPositionTag tag = new MarkedPositionTag();
+ tag.setPageContext(this.pc);
+ tag.setPathId(pathId);
+ int result = tag.doStartTag();
+ assertEquals(Tag.EVAL_BODY_INCLUDE, result);
+ String output =
((MockHttpServletResponse)this.pc.getResponse()).getContentAsString();
+ assertEquals("/test/forward/request/uri?p1=v1&p2=v2&p3=v3",
output);
+ }
+
+ @Override
+ public Class[] getFixtureClasses() {
+ return new Class[]{};
+ }
+
+ @Override
+ protected String[] getConfigLocations() {
+ return new String[]{
+ "classpath:org/lambico/spring/dao/hibernate/genericDao.xml",
+ "classpath:org/lambico/spring/dao/hibernate/applicationContextBase.xml",
+ "classpath:org/parancoe/web/parancoeBase.xml",
+ "classpath:spring-test.xml"};
+ }
+
+ private void markPosition(String pathId) throws JspException {
+ MarkPositionTag tag = new MarkPositionTag();
+ tag.setPageContext(this.pc);
+ tag.setPathId(pathId);
+ int result = tag.doStartTag();
+ }
+
+}
=======================================
--- /parancoe-web/src/main/java/org/parancoe/web/tag/MarkPositionTag.java
Sat Mar 26 10:21:46 2011
+++ /parancoe-web/src/main/java/org/parancoe/web/tag/MarkPositionTag.java
Thu Sep 20 10:12:43 2012
@@ -18,10 +18,11 @@
package org.parancoe.web.tag;
import java.io.IOException;
-import javax.servlet.http.HttpServletRequest;
+import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import org.apache.commons.lang.StringUtils;
+import org.parancoe.web.util.MarkPositionHelper;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
@@ -38,6 +39,10 @@
* The identifier of the path in which the position has been marked.
*/
protected String pathId = null;
+ /**
+ * If true, the tag generate full URIs (
http://host/..etc.)
+ */
+ protected boolean useFullUri;
public String getPathId() {
return pathId;
@@ -46,22 +51,37 @@
public void setPathId(String pathId) {
this.pathId = pathId;
}
+
+ public boolean isUseFullUri() {
+ return useFullUri;
+ }
+
+ public void setUseFullUri(boolean useFullUri) {
+ this.useFullUri = useFullUri;
+ }
@Override
protected final int doStartTagInternal() throws JspException,
IOException {
- final HttpServletRequest request = (HttpServletRequest)
pageContext.getRequest();
final HttpSession session = pageContext.getSession();
+ ServletRequest request = pageContext.getRequest();
final RequestContext requestContext = this.getRequestContext();
- final String uri =
requestContext.getUrlPathHelper().getOriginatingRequestUri(request);
+ final String uri = requestContext.getRequestUri();
final String queryString =
-
requestContext.getUrlPathHelper().getOriginatingQueryString(request);
+ requestContext.getQueryString();
String url = "";
if (StringUtils.isBlank(queryString)) {
url = uri;
} else {
url = uri + "?" + queryString;
}
- session.setAttribute(PREFIX + pathId, url);
+ if (useFullUri) {
+ String port = "";
+ if (request.getServerPort() != 80) {
+ port = ":"+request.getServerPort();
+ }
+ url =
request.getScheme()+"://"+request.getServerName()+port+requestContext.getContextPath()+url;
+ }
+ MarkPositionHelper.markPosition(session, pathId, url);
return SKIP_BODY;
}
}
=======================================
--- /parancoe-web/src/main/java/org/parancoe/web/tag/MarkedPositionTag.java
Sat Mar 26 10:21:46 2011
+++ /parancoe-web/src/main/java/org/parancoe/web/tag/MarkedPositionTag.java
Thu Sep 20 10:12:43 2012
@@ -20,6 +20,7 @@
import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
+import org.parancoe.web.util.MarkPositionHelper;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
/**
@@ -58,9 +59,9 @@
@Override
protected final int doStartTagInternal() throws JspException,
IOException {
final HttpSession session = pageContext.getSession();
- Object value = session.getAttribute(MarkPositionTag.PREFIX +
pathId);
+ String value = MarkPositionHelper.getMarkedPosition(session,
pathId);
if (value != null) {
- pageContext.getOut().write(value.toString());
+ pageContext.getOut().write(value);
} else {
if (defaultUrl != null) {
pageContext.getOut().write(defaultUrl);
=======================================
--- /parancoe-web/src/main/resources/META-INF/parancoe.tld Sat Mar 26
10:08:51 2011
+++ /parancoe-web/src/main/resources/META-INF/parancoe.tld Thu Sep 20
10:12:43 2012
@@ -36,6 +36,14 @@
<rtexprvalue>false</rtexprvalue>
<description>The id of the path for this marking.</description>
</attribute>
+
+ <attribute>
+ <name>useFullUri</name>
+ <required>false</required>
+ <rtexprvalue>false</rtexprvalue>
+ <description>If true, use complete URIs
(
http://host/...etc.).</description>
+ </attribute>
+
</tag>
<tag>