Author: cziegeler
Date: Thu Mar 25 08:43:19 2010
New Revision: 927305
URL: http://svn.apache.org/viewvc?rev=927305&view=rev
Log:
SLING-1461 : Move request and resource util to Sling API
Added:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
(with props)
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
(with props)
Modified:
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/RequestUtil.java
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/ResponseUtil.java
Added:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java?rev=927305&view=auto
==============================================================================
---
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
(added)
+++
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
Thu Mar 25 08:43:19 2010
@@ -0,0 +1,163 @@
+/*
+ * 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.api.request;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @since 2.1
+ */
+public class RequestUtil {
+
+ /**
+ * Parses a header of the form:
+ *
+ * <pre>
+ * Header = Token { "," Token } .
+ * Token = name { ";" Parameter } .
+ * Paramter = name [ "=" value ] .
+ * </pre>
+ *
+ * "," and ";" are not allowed within name and value
+ *
+ * @param value
+ * @return A Map indexed by the Token names where the values are Map
+ * instances indexed by parameter name
+ */
+ public static Map<String, Map<String, String>> parserHeader(String value) {
+ Map<String, Map<String, String>> result = new HashMap<String,
Map<String, String>>();
+ String[] tokens = value.split(",");
+ for (int i = 0; i < tokens.length; i++) {
+ String[] parameters = tokens[i].split(";");
+ String name = parameters[0].trim();
+ Map<String, String> parMap;
+ if (parameters.length > 0) {
+ parMap = new HashMap<String, String>();
+ for (int j = 1; j < parameters.length; j++) {
+ String[] content = parameters[j].split("=", 2);
+ if (content.length > 1) {
+ parMap.put(content[0].trim(), content[1].trim());
+ } else {
+ parMap.put(content[0].trim(), content[0].trim());
+ }
+ }
+ } else {
+ parMap = Collections.emptyMap();
+ }
+ result.put(name, parMap);
+ }
+ return result;
+ }
+
+ /**
+ * Parses an <code>Accept-*</code> header of the form:
+ *
+ * <pre>
+ * Header = Token { "," Token } .
+ * Token = name { ";" "q" [ "="
value ] } .
+ * Paramter = .
+ * </pre>
+ *
+ * "," and ";" are not allowed within name and value
+ *
+ * @param value
+ * @return A Map indexed by the Token names where the values are
+ * <code>Double</code> instances providing the value of the
+ * <code>q</code> parameter.
+ */
+ public static Map<String, Double> parserAcceptHeader(String value) {
+ Map<String, Double> result = new HashMap<String, Double>();
+ String[] tokens = value.split(",");
+ for (int i = 0; i < tokens.length; i++) {
+ String[] parameters = tokens[i].split(";");
+ String name = parameters[0];
+ Double qVal = new Double(1.0);
+ if (parameters.length > 1) {
+ for (int j = 1; j < parameters.length; j++) {
+ String[] content = parameters[j].split("=", 2);
+ if (content.length > 1 && "q".equals(content[0])) {
+ try {
+ qVal = Double.valueOf(content[1]);
+ } catch (NumberFormatException nfe) {
+ // don't care
+ }
+ }
+ }
+ }
+ if (qVal != null) {
+ result.put(name, qVal);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Utility method to return a name for the given servlet. This method
+ * applies the following algorithm to find a non-<code>null</code>,
+ * non-empty name:
+ * <ol>
+ * <li>If the servlet has a servlet config, the servlet name from the
+ * servlet config is taken.
+ * <li>Otherwise check the servlet info
+ * <li>Otherwise use the fully qualified name of the servlet class
+ * </ol>
+ */
+ public static String getServletName(Servlet servlet) {
+ String name = null;
+
+ if (servlet.getServletConfig() != null) {
+ name = servlet.getServletConfig().getServletName();
+ }
+ if (name == null || name.length() == 0) {
+ name = servlet.getServletInfo();
+ }
+ if (name == null || name.length() == 0) {
+ name = servlet.getClass().getName();
+ }
+
+ return name;
+ }
+
+ /**
+ * Sets the named request attribute to the new value and returns the
+ * previous value.
+ *
+ * @param request The request object whose attribute is to be set.
+ * @param name The name of the attribute to be set.
+ * @param value The new value of the attribute. If this is
<code>null</code>
+ * the attribte is actually removed from the request.
+ * @return The previous value of the named request attribute or
+ * <code>null</code> if it was not set.
+ */
+ public static Object setRequestAttribute(HttpServletRequest request,
+ String name, Object value) {
+ Object oldValue = request.getAttribute(name);
+ if (value == null) {
+ request.removeAttribute(name);
+ } else {
+ request.setAttribute(name, value);
+ }
+ return oldValue;
+ }
+}
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java?rev=927305&view=auto
==============================================================================
---
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
(added)
+++
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
Thu Mar 25 08:43:19 2010
@@ -0,0 +1,110 @@
+/*
+ * 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.api.request;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Response-related utilities
+ * @since 2.1
+ */
+public class ResponseUtil {
+
+ private static class XmlEscapingWriter extends Writer {
+ private final Writer target;
+
+ XmlEscapingWriter(Writer target) {
+ this.target = target;
+ }
+
+ @Override
+ public void close() throws IOException {
+ target.close();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ target.flush();
+ }
+
+ @Override
+ public void write(char[] buffer, int offset, int length) throws
IOException {
+ for(int i = offset; i < offset + length; i++) {
+ write(buffer[i]);
+ }
+ }
+
+ @Override
+ public void write(char[] cbuf) throws IOException {
+ write(cbuf, 0, cbuf.length);
+ }
+
+ @Override
+ public void write(int c) throws IOException {
+ if(c == '&') {
+ target.write("&");
+ } else if(c == '<') {
+ target.write("<");
+ } else if(c == '>') {
+ target.write(">");
+ } else {
+ target.write(c);
+ }
+ }
+
+ @Override
+ public void write(String str, int off, int len) throws IOException {
+ write(str.toCharArray(), off, len);
+ }
+
+ @Override
+ public void write(String str) throws IOException {
+ write(str.toCharArray());
+ }
+ }
+
+ /** Escape xml text */
+ public static String escapeXml(String input) {
+ if(input == null) {
+ return null;
+ }
+
+ final StringBuilder b = new StringBuilder(input.length());
+ for(int i = 0;i < input.length(); i++) {
+ final char c = input.charAt(i);
+ if(c == '&') {
+ b.append("&");
+ } else if(c == '<') {
+ b.append("<");
+ } else if(c == '>') {
+ b.append(">");
+ } else {
+ b.append(c);
+ }
+ }
+ return b.toString();
+ }
+
+ /** Return a Writer that writes escaped XML text to target
+ */
+ public static Writer getXmlEscapingWriter(Writer target) {
+ return new XmlEscapingWriter(target);
+ }
+}
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange:
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/ResponseUtil.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/RequestUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/RequestUtil.java?rev=927305&r1=927304&r2=927305&view=diff
==============================================================================
---
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/RequestUtil.java
(original)
+++
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/RequestUtil.java
Thu Mar 25 08:43:19 2010
@@ -25,19 +25,23 @@ import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
+/**
+ * @deprecated Use {...@link org.apache.sling.api.request.RequestUtil}
+ */
+...@deprecated
public class RequestUtil {
/**
* Parses a header of the form:
- *
+ *
* <pre>
* Header = Token { "," Token } .
* Token = name { ";" Parameter } .
* Paramter = name [ "=" value ] .
* </pre>
- *
+ *
* "," and ";" are not allowed within name and value
- *
+ *
* @param value
* @return A Map indexed by the Token names where the values are Map
* instances indexed by parameter name
@@ -69,15 +73,15 @@ public class RequestUtil {
/**
* Parses an <code>Accept-*</code> header of the form:
- *
+ *
* <pre>
* Header = Token { "," Token } .
* Token = name { ";" "q" [ "="
value ] } .
* Paramter = .
* </pre>
- *
+ *
* "," and ";" are not allowed within name and value
- *
+ *
* @param value
* @return A Map indexed by the Token names where the values are
* <code>Double</code> instances providing the value of the
@@ -139,7 +143,7 @@ public class RequestUtil {
/**
* Sets the named request attribute to the new value and returns the
* previous value.
- *
+ *
* @param request The request object whose attribute is to be set.
* @param name The name of the attribute to be set.
* @param value The new value of the attribute. If this is
<code>null</code>
Modified:
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/ResponseUtil.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/ResponseUtil.java?rev=927305&r1=927304&r2=927305&view=diff
==============================================================================
---
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/ResponseUtil.java
(original)
+++
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/ResponseUtil.java
Thu Mar 25 08:43:19 2010
@@ -21,7 +21,11 @@ package org.apache.sling.engine;
import java.io.IOException;
import java.io.Writer;
-/** Response-related utilities */
+/**
+ * Response-related utilities.
+ * @deprecated Use {...@link org.apache.sling.api.request.ResponseUtil}
+ */
+...@deprecated
public class ResponseUtil {
private static class XmlEscapingWriter extends Writer {