Author: bdelacretaz
Date: Fri Dec 7 06:35:32 2007
New Revision: 602108
URL: http://svn.apache.org/viewvc?rev=602108&view=rev
Log:
SLING-126 - MicrojaxPostServlet: redirect to Referer if provided, unless
ujax_redirect is provided
Added:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
(with props)
Modified:
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/helpers/MicroslingIntegrationTestClient.java
Modified:
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java?rev=602108&r1=602107&r2=602108&view=diff
==============================================================================
---
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
(original)
+++
incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Fri Dec 7 06:35:32 2007
@@ -191,34 +191,46 @@
// walk the request parameters, create and save nodes and properties
setPropertiesFromRequest(currentNode, request, savePrefix,
createdNodes);
- // sava data and find out where to redirect
+ // sava data and send redirect
s.save();
- final String forcedRedirect = request.getParameter(RP_REDIRECT_TO);
- final String redirectExtension =
request.getParameter(RP_DISPLAY_EXTENSION);
-
- // Where to redirect to when done
- String redirectPath;
- if(forcedRedirect != null) {
- redirectPath = forcedRedirect;
- } else {
- redirectPath = currentNode.getPath();
+ response.sendRedirect(getRedirectUrl(request,currentNode.getPath()));
+ }
+
+ /** compute redirect URL (SLING-126) */
+ protected String getRedirectUrl(SlingHttpServletRequest request, String
currentNodePath) {
+
+ // redirect param has priority
+ String result = request.getParameter(RP_REDIRECT_TO);
+
+ if(result==null || result.trim().length()==0) {
+ // try Referer
+ result = request.getHeader("Referer");
}
- if(redirectExtension!=null) {
- if(redirectExtension.startsWith(".")) {
- redirectPath += redirectExtension;
- } else {
- redirectPath += "." + redirectExtension;
+
+ if(result==null || result.trim().length()==0) {
+ // use path of current node, with optional extension
+ final String redirectExtension =
request.getParameter(RP_DISPLAY_EXTENSION);
+ result = currentNodePath;
+
+ if(redirectExtension!=null) {
+ if(redirectExtension.startsWith(".")) {
+ result += redirectExtension;
+ } else {
+ result += "." + redirectExtension;
+ }
}
+
+ result =
+ SlingRequestPaths.getContextPath(request)
+ + SlingRequestPaths.getServletPath(request)
+ + result;
}
-
- final String redirectUrl =
- SlingRequestPaths.getContextPath(request)
- + SlingRequestPaths.getServletPath(request)
- + redirectPath;
+
if(log.isDebugEnabled()) {
- log.debug("Redirecting to " + redirectUrl);
+ log.debug("Will redirect to " + result);
}
- response.sendRedirect(redirectUrl);
+
+ return result;
}
/** Set Node properties from current request
Added:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java?rev=602108&view=auto
==============================================================================
---
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
(added)
+++
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
Fri Dec 7 06:35:32 2007
@@ -0,0 +1,54 @@
+/*
+ * 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.sling.microsling.integration;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+
+/** Test the various redirect options for POST, SLING-126 */
+public class PostRedirectTest extends MicroslingHttpTestBase {
+
+ private String postPath = "CreateNodeTest/" + System.currentTimeMillis();
+ private String postUrl = HTTP_BASE_URL + "/" + postPath + "/*";
+
+ public void testForcedRedirect() throws IOException {
+ final Map<String,String> params = new HashMap<String,String>();
+ params.put("ujax_redirect","http://forced/");
+ final Map<String,String> headers = new HashMap<String,String>();
+ headers.put("Referer", "http://referer/");
+
+ final String location = testClient.createNode(postUrl, params,
headers);
+ assertEquals("With forced redirect and Referer, redirect must be
forced","http://forced/",location);
+ }
+
+ public void testDefaultRedirect() throws IOException {
+ final String location = testClient.createNode(postUrl, null, null);
+ assertTrue(
+ "With no headers or parameters, redirect (" + location
+ + ") must point to created node (path=" + postPath + ")",
+ location.contains(postPath));
+ }
+
+ public void testRefererRedirect() throws IOException {
+ final Map<String,String> headers = new HashMap<String,String>();
+ headers.put("Referer", "http://referer/");
+ final String location = testClient.createNode(postUrl, null, headers);
+ assertEquals("With Referer, redirect must point to
referer","http://referer/",location);
+ }
+}
\ No newline at end of file
Propchange:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/PostRedirectTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified:
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/helpers/MicroslingIntegrationTestClient.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/helpers/MicroslingIntegrationTestClient.java?rev=602108&r1=602107&r2=602108&view=diff
==============================================================================
---
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/helpers/MicroslingIntegrationTestClient.java
(original)
+++
incubator/sling/trunk/microsling/microsling-core/src/test/java/org/apache/sling/microsling/integration/helpers/MicroslingIntegrationTestClient.java
Fri Dec 7 06:35:32 2007
@@ -86,18 +86,30 @@
throw new IOException("Expected status 200, got " + status + " for
URL=" + url);
}
}
+
+ /** Call the other createNode method with headers==null */
+ public String createNode(String url, Map<String,String> nodeProperties)
throws IOException {
+ return createNode(url, nodeProperties, null);
+ }
/** Create a node under given path, using a POST to microsling
* @param url under which node is created
* @return the URL that microsling provides to display the node
*/
- public String createNode(String url, Map<String,String> nodeProperties)
throws IOException {
+ public String createNode(String url, Map<String,String> nodeProperties,
Map<String,String> requestHeaders)
+ throws IOException {
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
- if(nodeProperties!=null) {
+ if(nodeProperties != null) {
for(Map.Entry<String,String> e : nodeProperties.entrySet()) {
post.addParameter(e.getKey(),e.getValue());
+ }
+ }
+
+ if(requestHeaders != null) {
+ for(Map.Entry<String,String> e : requestHeaders.entrySet()) {
+ post.addRequestHeader(e.getKey(), e.getValue());
}
}