From: <fg...@us...> - 2007-06-30 15:04:54
|
Revision: 341 http://svn.sourceforge.net/openutils/?rev=341&view=rev Author: fgiust Date: 2007-06-30 08:04:52 -0700 (Sat, 30 Jun 2007) Log Message: ----------- initial import Added Paths: ----------- trunk/openutils-magnolia2mvcbridge/ trunk/openutils-magnolia2mvcbridge/pom.xml trunk/openutils-magnolia2mvcbridge/src/ trunk/openutils-magnolia2mvcbridge/src/main/ trunk/openutils-magnolia2mvcbridge/src/main/java/ trunk/openutils-magnolia2mvcbridge/src/main/java/it/ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ContentBridgeTag.java trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/MagnoliaMultipartResolver.java trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/RewriteVarsThreadLocal.java trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/UrlFunctions.java trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/WrappedResponse.java trunk/openutils-magnolia2mvcbridge/src/main/resources/ trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/ trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/magnolia-bridge-tags.tld trunk/openutils-magnolia2mvcbridge/src/test/ trunk/openutils-magnolia2mvcbridge/src/test/java/ trunk/openutils-magnolia2mvcbridge/src/test/java/it/ trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/ trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ContentBridgeTagTest.java Property changes on: trunk/openutils-magnolia2mvcbridge ___________________________________________________________________ Name: svn:ignore + target .checkstyle .classpath .project .settings Added: trunk/openutils-magnolia2mvcbridge/pom.xml =================================================================== --- trunk/openutils-magnolia2mvcbridge/pom.xml (rev 0) +++ trunk/openutils-magnolia2mvcbridge/pom.xml 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>3</version> + <relativePath>..</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <packaging>jar</packaging> + <groupId>it.openmindonline.magnolia</groupId> + <artifactId>openutils-magnolia2mvcbridge</artifactId> + <name>openutils-magnolia2mvcbridge</name> + <version>1.0-SNAPSHOT</version> + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.3.0</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <version>2.0.6</version> + </dependency> + <dependency> + <groupId>info.magnolia</groupId> + <artifactId>magnolia-core</artifactId> + <version>3.0.2</version> + </dependency> + <dependency> + <groupId>info.magnolia</groupId> + <artifactId>magnolia-jaas</artifactId> + <version>3.0.2</version> + </dependency> + <dependency> + <groupId>opensymphony</groupId> + <artifactId>sitemesh</artifactId> + <version>2.3</version> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.4</version> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>jsp-api</artifactId> + <version>2.0</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <classifier>jdk15</classifier> + <version>5.1</version> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> +</project> Property changes on: trunk/openutils-magnolia2mvcbridge/pom.xml ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ContentBridgeTag.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ContentBridgeTag.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ContentBridgeTag.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,193 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +import info.magnolia.cms.beans.config.ContentRepository; +import info.magnolia.cms.security.AccessManager; +import info.magnolia.cms.security.Authenticator; +import info.magnolia.cms.security.Permission; +import info.magnolia.cms.security.SecureURI; +import info.magnolia.context.MgnlContext; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.JspWriter; +import javax.servlet.jsp.tagext.TagSupport; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.opensymphony.module.sitemesh.Page; +import com.opensymphony.module.sitemesh.parser.FastPageParser; + + +/** + * @author fgiust + * @version $Id$ + */ +public class ContentBridgeTag extends TagSupport +{ + + /** + * Stable serialVersionUID. + */ + private static final long serialVersionUID = 42L; + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(ContentBridgeTag.class); + + /** + * Url to be included. + */ + private String url; + + /** + * Sets the url. + * @param url the url to set + */ + public void setUrl(String url) + { + this.url = url; + } + + protected String makeUrl(String contextPath, String requestURI, String actionParam, String baseActionUrl) + { + + StringBuffer currentUrl = new StringBuffer(); + currentUrl.append(requestURI); + + String actionUrl = actionParam; + + if (actionUrl != null && !actionUrl.startsWith("/")) + { + + if (!StringUtils.equals(actionUrl, baseActionUrl) && StringUtils.contains(baseActionUrl, "/")) + { + actionUrl = StringUtils.substringBeforeLast(baseActionUrl, "/") + "/" + actionUrl; + } + + if (!actionUrl.startsWith("/")) + { + actionUrl = "/" + actionUrl; + } + } + + RewriteVarsThreadLocal.setContextPath(contextPath); + RewriteVarsThreadLocal.setCurrentPageUrl(currentUrl.toString()); + + return actionUrl; + + } + + /** + * {@inheritDoc} + */ + @Override + public int doStartTag() throws JspException + { + + HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); + HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); + + String requestURI = (String) request.getAttribute("javax.servlet.forward.request_uri"); + + if (requestURI == null) + { + requestURI = request.getRequestURI(); + } + + String actionParam = url; + + if (request.getParameter("_action") != null) + { + actionParam = request.getParameter("_action"); + } + + if (requestURI.startsWith("//")) // buggy url set in javax.servlet.forward.request_uri? + { + requestURI = requestURI.substring(1); + } + + String actionUrl = makeUrl(request.getContextPath(), requestURI, actionParam, url); + + if (!StringUtils.contains(actionUrl, "?")) + { + actionUrl = StringUtils.replaceOnce(actionUrl, "&", "?"); + } + + if (SecureURI.isProtected(actionUrl)) + { + AccessManager am = MgnlContext.getAccessManager(ContentRepository.WEBSITE); + if (!Authenticator.isAuthenticated(request) || !am.isGranted(actionUrl, Permission.READ)) + { + log.info("User not allowed to read path {}", actionUrl); + return super.doStartTag(); + } + } + + RequestDispatcher rd = pageContext.getServletConfig().getServletContext().getRequestDispatcher(actionUrl); + + WrappedResponse wresponse = new WrappedResponse(response); + JspWriter out = pageContext.getOut(); + try + { + rd.include(request, wresponse); + } + catch (ServletException e) + { + log.error("error including " + actionUrl + ": " + e.getMessage(), e); + e.printStackTrace(new PrintWriter(out)); + } + catch (IOException e) + { + log.error("error including " + actionUrl + ": " + e.getMessage(), e); + e.printStackTrace(new PrintWriter(out)); + } + finally + { + RewriteVarsThreadLocal.setCurrentPageUrl(null); + RewriteVarsThreadLocal.setContextPath(null); + } + + char[] content = wresponse.getContent(); + + if (content != null && content.length > 0) + { + FastPageParser parser = new FastPageParser(); + try + { + Page page = parser.parse(content); + page.writeBody(out); + } + catch (IOException e) + { + log.error("error parsing " + actionUrl + ": " + e.getMessage(), e); + e.printStackTrace(new PrintWriter(out)); + } + } + + return super.doStartTag(); + } +} Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/ContentBridgeTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/MagnoliaMultipartResolver.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/MagnoliaMultipartResolver.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/MagnoliaMultipartResolver.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,219 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +import info.magnolia.cms.beans.runtime.Document; +import info.magnolia.cms.beans.runtime.MultipartForm; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.FileUpload; +import org.apache.commons.fileupload.FileUploadBase; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.fileupload.servlet.ServletRequestContext; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.springframework.web.context.ServletContextAware; +import org.springframework.web.multipart.MultipartException; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; +import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsFileUploadSupport; +import org.springframework.web.multipart.commons.CommonsMultipartFile; +import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest; +import org.springframework.web.util.WebUtils; + + +/** + * Spring MagnoliaMultipartResolver which interacts with magnolia. + * @author fgiust + * @version $Id: $ + */ +public class MagnoliaMultipartResolver extends CommonsFileUploadSupport + implements + MultipartResolver, + ServletContextAware +{ + + /** + * Constructor for use as bean. Determines the servlet container's temporary directory via the ServletContext passed + * in as through the ServletContextAware interface (typically by a WebApplicationContext). + * @see #setServletContext + * @see org.springframework.web.context.ServletContextAware + * @see org.springframework.web.context.WebApplicationContext + */ + public MagnoliaMultipartResolver() + { + super(); + } + + /** + * Constructor for standalone usage. Determines the servlet container's temporary directory via the given + * ServletContext. + * @param servletContext the ServletContext to use + */ + public MagnoliaMultipartResolver(ServletContext servletContext) + { + this(); + setServletContext(servletContext); + } + + /** + * Initialize the underlying <code>org.apache.commons.fileupload.servlet.ServletFileUpload</code> instance. Can be + * overridden to use a custom subclass, e.g. for testing purposes. + * @param fileItemFactory the Commons FileItemFactory to use + * @return the new ServletFileUpload instance + */ + @Override + protected FileUpload newFileUpload(FileItemFactory fileItemFactory) + { + return new ServletFileUpload(fileItemFactory); + } + + public void setServletContext(ServletContext servletContext) + { + if (!isUploadTempDirSpecified()) + { + getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext)); + } + } + + public boolean isMultipart(HttpServletRequest request) + { + return FileUploadBase.isMultipartContent(new ServletRequestContext(request)); + } + + @SuppressWarnings("unchecked") + public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException + { + // already parsed by magnolia + MultipartForm form = (MultipartForm) request.getAttribute("multipartform"); + + Map<String, Document> documents = form.getDocuments(); + Map<String, MultipartFile> files = new HashMap<String, MultipartFile>(); + + for (String key : documents.keySet()) + { + Document doc = documents.get(key); + files.put(key, new DocWrapperMultipartFile(doc)); + } + + Map<String, String> singleParamters = form.getParameters(); + Map<String, String[]> multiParamters = new HashMap<String, String[]>(); + + for (String key : singleParamters.keySet()) + { + multiParamters.put(key, new String[]{singleParamters.get(key) }); + } + + return new DefaultMultipartHttpServletRequest(request, files, multiParamters); + + } + + public static class DocWrapperMultipartFile implements MultipartFile + { + + Document document; + + public DocWrapperMultipartFile(Document document) + { + this.document = document; + } + + /** + * {@inheritDoc} + */ + public byte[] getBytes() throws IOException + { + return IOUtils.toByteArray(getInputStream()); + } + + /** + * {@inheritDoc} + */ + public String getContentType() + { + return document.getType(); + } + + /** + * {@inheritDoc} + */ + public InputStream getInputStream() throws IOException + { + return document.getStream(); + } + + /** + * {@inheritDoc} + */ + public String getName() + { + return document.getFileNameWithExtension(); + } + + /** + * {@inheritDoc} + */ + public String getOriginalFilename() + { + return document.getFileNameWithExtension(); + } + + /** + * {@inheritDoc} + */ + public long getSize() + { + return document.getLength(); + } + + /** + * {@inheritDoc} + */ + public boolean isEmpty() + { + return document.getLength() == 0; + } + + /** + * {@inheritDoc} + */ + public void transferTo(File dest) throws IOException, IllegalStateException + { + FileUtils.copyFile(document.getFile(), dest); + } + + } + + /** + * {@inheritDoc} + */ + public void cleanupMultipart(MultipartHttpServletRequest request) + { + // nothing to do + + } + +} Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/MagnoliaMultipartResolver.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/RewriteVarsThreadLocal.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/RewriteVarsThreadLocal.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/RewriteVarsThreadLocal.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,73 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +/** + * @author fgiust + * @version $Id$ + */ +public final class RewriteVarsThreadLocal +{ + + private static ThreadLocal<String> currentPageUrl = new ThreadLocal<String>(); + + private static ThreadLocal<String> contextPath = new ThreadLocal<String>(); + + /** + * Don't instantiate. + */ + private RewriteVarsThreadLocal() + { + // unused + } + + /** + * Returns the currentPageUrl. + * @return the currentPageUrl + */ + public static String getCurrentPageUrl() + { + return currentPageUrl.get(); + } + + /** + * Sets the currentPageUrl. + * @param url the currentPageUrl to set + */ + public static void setCurrentPageUrl(String url) + { + currentPageUrl.set(url); + } + + /** + * Returns the contextPath. + * @return the contextPath + */ + public static String getContextPath() + { + return contextPath.get(); + } + + /** + * Sets the contextPath. + * @param context the contextPath to set + */ + public static void setContextPath(String context) + { + contextPath.set(context); + } + +} Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/RewriteVarsThreadLocal.java ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/UrlFunctions.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/UrlFunctions.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/UrlFunctions.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,105 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +import info.magnolia.context.MgnlContext; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; + + +/** + * @author fgiust + * @version $Id$ + */ +public final class UrlFunctions +{ + + /** + * Don't instantiate. + */ + private UrlFunctions() + { + // unused + } + + public static String url(String url) + { + return encodeUrl(RewriteVarsThreadLocal.getCurrentPageUrl(), url); + } + + public static String urlWithDestination(String url, String destination) + { + // check RewriteVarsThreadLocal because if not running under magnolia we should not replace urls + if (RewriteVarsThreadLocal.getCurrentPageUrl() == null || destination == null) + { + return url(url); + } + + return encodeUrl(RewriteVarsThreadLocal.getContextPath() + destination, url); + } + + /** + * @param baseUrl + * @param actionUrl + * @return + */ + private static String encodeUrl(final String baseUrl, String actionUrl) + { + String contextPath = MgnlContext.getContextPath(); + if (StringUtils.isNotEmpty(contextPath) && actionUrl != null && actionUrl.startsWith(contextPath)) + { + actionUrl = StringUtils.substringAfter(actionUrl, contextPath); + } + + if (baseUrl == null) + { + if (actionUrl.startsWith("/")) + { + return contextPath + actionUrl; + } + + return actionUrl; + } + + int paramIndex = baseUrl.indexOf('?'); + String page = baseUrl; + String params = null; + if (paramIndex != -1) + { + page = StringUtils.substring(baseUrl, 0, paramIndex + 1); + params = StringUtils.substring(baseUrl, paramIndex + 1); + } + + StringBuffer outUrl = new StringBuffer(); + outUrl.append(page); + outUrl.append("?"); + + if (params != null) + { + outUrl.append(params); + outUrl.append("&"); + } + + outUrl.append("_action="); + outUrl.append(StringUtils.replace(actionUrl, "?", "&")); + + String encodedUrl = StringEscapeUtils.escapeXml(outUrl.toString()); + + return encodedUrl; + } + +} Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/UrlFunctions.java ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/WrappedResponse.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/WrappedResponse.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/WrappedResponse.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,130 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +import java.io.ByteArrayOutputStream; +import java.io.CharArrayWriter; +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + + +/** + * @author fgiust + * @version $Id$ + */ +public class WrappedResponse extends HttpServletResponseWrapper +{ + + /** + * The buffered response. + */ + private CharArrayWriter outputWriter; + + /** + * The outputWriter stream. + */ + private SimpleServletOutputStream servletOutputStream; + + /** + * @param httpServletResponse the response to wrap + */ + public WrappedResponse(HttpServletResponse httpServletResponse) + { + super(httpServletResponse); + this.outputWriter = new CharArrayWriter(); + this.servletOutputStream = new SimpleServletOutputStream(); + } + + /** + * {@inheritDoc} + */ + @Override + public PrintWriter getWriter() throws IOException + { + return new PrintWriter(this.outputWriter); + } + + /** + * Flush the buffer, not the response. + * @throws IOException if encountered when flushing + */ + @Override + public void flushBuffer() throws IOException + { + if (outputWriter != null) + { + this.outputWriter.flush(); + this.servletOutputStream.outputStream.reset(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public ServletOutputStream getOutputStream() throws IOException + { + return this.servletOutputStream; + } + + /** + * @return buffered response + */ + public char[] getContent() + { + return this.outputWriter.toCharArray(); + } + + class SimpleServletOutputStream extends ServletOutputStream + { + + /** + * My outputWriter stream, a buffer. + */ + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + /** + * {@inheritDoc} + */ + @Override + public void write(int b) + { + this.outputStream.write(b); + } + + /** + * {@inheritDoc} Get the contents of the outputStream. + * @return contents of the outputStream + */ + @Override + public String toString() + { + return this.outputStream.toString(); + } + + /** + * Reset the wrapped ByteArrayOutputStream. + */ + public void reset() + { + outputStream.reset(); + } + } +} \ No newline at end of file Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/java/it/openutils/mvcbridge/WrappedResponse.java ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/magnolia-bridge-tags.tld =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/magnolia-bridge-tags.tld (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/magnolia-bridge-tags.tld 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,30 @@ +<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" + version="2.0"> + <description>magnolia-bridge</description> + <display-name>magnolia-bridge</display-name> + <tlib-version>1.2</tlib-version> + <short-name>magnolia-bridge</short-name> + <uri>magnolia-bridge</uri> + <tag> + <description>spring</description> + <name>spring</name> + <tag-class>it.openutils.mvcbridge.ContentBridgeTag</tag-class> + <body-content>empty</body-content> + <attribute> + <name>url</name> + <required>true</required> + <rtexprvalue>true</rtexprvalue> + </attribute> + </tag> + <function> + <name>url</name> + <function-class>it.openutils.mvcbridge.UrlFunctions</function-class> + <function-signature>java.lang.String url(java.lang.String)</function-signature> + </function> + <function> + <name>urlto</name> + <function-class>it.openutils.mvcbridge.UrlFunctions</function-class> + <function-signature>java.lang.String urlWithDestination(java.lang.String, java.lang.String)</function-signature> + </function> +</taglib> \ No newline at end of file Property changes on: trunk/openutils-magnolia2mvcbridge/src/main/resources/META-INF/magnolia-bridge-tags.tld ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + text/xml Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ContentBridgeTagTest.java =================================================================== --- trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ContentBridgeTagTest.java (rev 0) +++ trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ContentBridgeTagTest.java 2007-06-30 15:04:52 UTC (rev 341) @@ -0,0 +1,97 @@ +/* + * Copyright 2007 Fabrizio Giustina. + * + * 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 it.openutils.mvcbridge; + +import info.magnolia.context.MgnlContext; +import info.magnolia.context.SystemContextImpl; +import it.openutils.mvcbridge.ContentBridgeTag; +import it.openutils.mvcbridge.RewriteVarsThreadLocal; +import it.openutils.mvcbridge.UrlFunctions; + +import org.testng.Assert; +import org.testng.annotations.Test; + + +/** + * @author fgiust + * @version $Id$ + */ +public class ContentBridgeTagTest +{ + + private ContentBridgeTag tag = new ContentBridgeTag(); + + @Test + public void testRewriteSimple() + { + MgnlContext.setInstance(new SystemContextImpl()); + + String action = tag.makeUrl("", "/test/it.html", "/spring/myaction.do", "/spring/start.do"); + Assert.assertEquals(action, "/spring/myaction.do"); // <!-- include this + + String pageUrl = RewriteVarsThreadLocal.getCurrentPageUrl(); + Assert.assertEquals(pageUrl, "/test/it.html"); + + String finalUrl = UrlFunctions.url("/spring/pippo.do"); + Assert.assertEquals(finalUrl, "/test/it.html?_action=/spring/pippo.do"); + } + + @Test + public void testRewriteNoPath() + { + MgnlContext.setInstance(new SystemContextImpl()); + + String action = tag.makeUrl("", "/test/it.html", "myaction.do", "/spring/start.do"); + Assert.assertEquals(action, "/spring/myaction.do"); // <!-- include this + + String pageUrl = RewriteVarsThreadLocal.getCurrentPageUrl(); + Assert.assertEquals(pageUrl, "/test/it.html"); + + String finalUrl = UrlFunctions.url("/spring/pippo.do"); + Assert.assertEquals(finalUrl, "/test/it.html?_action=/spring/pippo.do"); + } + + @Test + public void testRewriteParams() + { + MgnlContext.setInstance(new SystemContextImpl()); + + String action = tag.makeUrl("", "/test/it.html", "myaction.do", "/spring/start.do"); + Assert.assertEquals(action, "/spring/myaction.do"); // <!-- include this + + String pageUrl = RewriteVarsThreadLocal.getCurrentPageUrl(); + Assert.assertEquals(pageUrl, "/test/it.html"); + + String finalUrl = UrlFunctions.url("/spring/pippo.do?new=1&old=2"); + Assert.assertEquals(finalUrl, "/test/it.html?_action=/spring/pippo.do&new=1&old=2"); + } + + @Test + public void testRewriteActionParams() + { + MgnlContext.setInstance(new SystemContextImpl()); + + String action = tag.makeUrl("", "/mac/it/index.html", "iscrizioneEvento.do?idEvento=2", "/spring/start.do"); + Assert.assertEquals(action, "/spring/iscrizioneEvento.do?idEvento=2"); // <!-- include this + + String pageUrl = RewriteVarsThreadLocal.getCurrentPageUrl(); + Assert.assertEquals(pageUrl, "/mac/it/index.html"); + + String finalUrl = UrlFunctions.url("iscrizioneEvento.do?idEvento=2"); + Assert.assertEquals(finalUrl, "/mac/it/index.html?_action=iscrizioneEvento.do&idEvento=2"); + } + +} Property changes on: trunk/openutils-magnolia2mvcbridge/src/test/java/it/openutils/mvcbridge/ContentBridgeTagTest.java ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |