Author: richard.opalka(a)jboss.com
Date: 2007-11-09 11:26:48 -0500 (Fri, 09 Nov 2007)
New Revision: 5024
Added:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Constant.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Util.java
Removed:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/utils/
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/EndpointEmulator.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ObjectFactory.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Request.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/RequestImpl.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Response.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ResponseImpl.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/View.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ViewImpl.java
Log:
emulator minor refactoring
Added:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Constant.java
===================================================================
--- stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Constant.java
(rev 0)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Constant.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -0,0 +1,70 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.ws.jaxws.wsrm.emulator;
+
+/**
+ * Emulator framework constants
+ *
+ * @author richard.opalka(a)jboss.com
+ *
+ * @since Nov 9, 2007
+ */
+public final class Constant
+{
+
+ private Constant()
+ {
+ // forbidden inheritance
+ }
+
+ public static final String CONFIG_FILE = "config.file";
+ public static final String SEPARATOR = "|";
+ public static final String PARAGRAPH = "$";
+ public static final String LEFT_BRACKET = "{";
+ public static final String RIGHT_BRACKET = "}";
+ public static final String HTTP_POST = "POST";
+ public static final String HTTP_GET = "GET";
+ public static final String EQUAL = "=";
+ public static final String COMMA = ",";
+ public static final String SPACE = " ";
+ public static final String PROPERTIES = "properties";
+ public static final String MATCHES = "matches";
+ // XML configuration elements and attributes
+ public static final String NAMESPACES_ELEMENT = "namespaces";
+ public static final String NAMESPACE_ELEMENT = "namespace";
+ public static final String VIEW_ELEMENT = "view";
+ public static final String REQUEST_ELEMENT = "request";
+ public static final String RESPONSE_ELEMENT = "response";
+ public static final String SET_ELEMENT = "set";
+ public static final String CONTAINS_ELEMENT = "contains";
+ public static final String NODE_ELEMENT = "node";
+ public static final String ID_ATTRIBUTE = "id";
+ public static final String NAME_ATTRIBUTE = "name";
+ public static final String RESOURCE_ATTRIBUTE = "resource";
+ public static final String STATUS_CODE_ATTRIBUTE = "statusCode";
+ public static final String CONTENT_TYPE_ATTRIBUTE = "contentType";
+ public static final String PROPERTY_ATTRIBUTE = "property";
+ public static final String VALUE_ATTRIBUTE = "value";
+ public static final String HTTP_METHOD_ATTRIBUTE = "httpMethod";
+ public static final String PATH_INFO_ATTRIBUTE = "pathInfo";
+
+}
Property changes on:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Constant.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/EndpointEmulator.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/EndpointEmulator.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/EndpointEmulator.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -21,36 +21,23 @@
*/
package org.jboss.test.ws.jaxws.wsrm.emulator;
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import static org.jboss.test.ws.jaxws.wsrm.emulator.Constant.*;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import javax.xml.namespace.QName;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import org.jboss.test.ws.jaxws.wsrm.emulator.config.ObjectFactory;
import org.jboss.test.ws.jaxws.wsrm.emulator.config.View;
-import org.jboss.test.ws.jaxws.wsrm.emulator.utils.StringUtil;
-import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
/**
* This class represents Controller part of the MVC architectural pattern
@@ -62,7 +49,8 @@
public class EndpointEmulator extends HttpServlet
{
- private static final String CONFIG_FILE = "config.file";
+ private String configFile;
+ private ServletContext ctx;
private List<View> views;
@Override
@@ -70,268 +58,65 @@
{
super.init(config);
+ // initialize context variable
+ ctx = getServletContext();
+
// ensure there's a config.file servlet init parameter specified in web.xml
- String configFile = config.getInitParameter(CONFIG_FILE);
+ configFile = config.getInitParameter(CONFIG_FILE);
if (configFile == null)
throw new RuntimeException(CONFIG_FILE + " init parameter is
missing");
// ensure this config.file points to correct resource inside war archive
- InputStream is = config.getServletContext().getResourceAsStream(configFile);
+ InputStream is = ctx.getResourceAsStream(configFile);
if (is == null)
- throw new RuntimeException("Resource '" + configFile + "'
not found");
+ throw new RuntimeException("Resource " + configFile + " not
found");
- Element root = getDocument(is, false).getDocumentElement();
- views = getViews(root, getNamespaces(root));
-
- System.out.println(views);
+ Element root = Util.getDocument(is, false).getDocumentElement();
+ views = Util.getViews(root, Util.getNamespaces(root));
}
@Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+ protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
- handleRequest("POST", req, resp);
+ handleRequest(HTTP_POST, req, res);
}
@Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
- handleRequest("GET", req, resp);
+ handleRequest(HTTP_GET, req, res);
}
- private static Document getDocument(InputStream is, boolean nsAware) throws
ServletException
- {
- try
- {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(nsAware);
- return factory.newDocumentBuilder().parse(is);
- }
- catch (SAXException se)
- {
- throw new ServletException(se);
- }
- catch (ParserConfigurationException pce)
- {
- throw new ServletException(pce);
- }
- catch (IOException ie)
- {
- throw new ServletException(ie);
- }
- }
-
- private static Map<String, String> getNamespaces(Element root)
- {
- NodeList namespaces =
((Element)root.getElementsByTagName("namespaces").item(0)).getElementsByTagName("namespace");
- Map<String, String> retVal = new HashMap<String, String>();
- for (int i = 0; i < namespaces.getLength(); i++)
- {
- Element namespace = (Element)namespaces.item(i);
- retVal.put(namespace.getAttribute("name"),
namespace.getAttribute("value"));
- }
- return retVal;
- }
-
- private static List<View> getViews(Element root, Map<String, String>
namespaces)
- {
- List<View> retVal = new LinkedList<View>();
- NodeList views = root.getElementsByTagName("view");
- for (int i = 0; i < views.getLength(); i++)
- {
- retVal.add(ObjectFactory.getView((Element)views.item(i), namespaces));
- }
- return retVal;
- }
-
private void handleRequest(String httpMethod, HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
- String requestMessage = getRequestMessage(req);
- View view = getView(httpMethod, requestMessage);
- System.out.println("Handling view: " + view.getId());
+ String requestMessage = Util.getRequestMessage(req);
+ View view = Util.getView(httpMethod, requestMessage, views);
+ ctx.log(configFile + SEPARATOR + view.getId());
Map<String, String> resProperties = view.getResponse().getProperties();
Map<String, String> reqProperties = view.getRequest().getProperties();
- String responseMessage = getResource(view.getResponse().getResource());
+ String responseMessage = Util.getResourceAsString(ctx,
view.getResponse().getResource());
if (resProperties.size() > 0)
{
Map<String, String> initializedReqProperties = null;
if (reqProperties.size() > 0)
{
- initializedReqProperties = initializeProperties(requestMessage,
reqProperties);
+ initializedReqProperties = Util.initializeProperties(requestMessage,
reqProperties);
}
- Map<String, String> replaceMap =
replaceProperties(initializedReqProperties, resProperties);
- responseMessage = modifyResponse(responseMessage, replaceMap);
+ Map<String, String> replaceMap =
Util.prepareReplaceMap(initializedReqProperties, resProperties);
+ responseMessage = Util.replaceAll(responseMessage, replaceMap);
}
resp.setContentType(view.getResponse().getContentType());
+ resp.setStatus(Integer.valueOf(view.getResponse().getStatusCode()));
PrintWriter writer = resp.getWriter();
writer.print(responseMessage);
writer.flush();
writer.close();
}
- private Map<String, String> initializeProperties(String req, Map<String,
String> map)
- throws ServletException, IOException
- {
- Document requestMessage = getDocument(new ByteArrayInputStream(req.getBytes()),
true);
-
- Map<String, String> retVal = new HashMap<String, String>();
- for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); )
- {
- String key = i.next();
- String val = map.get(key);
- Element e = getElement(requestMessage, val);
- retVal.put(key, e.getTextContent());
- }
-
- return retVal;
- }
-
- private static Map<String, String> replaceProperties(Map<String, String>
reqM, Map<String, String> resM)
- {
- Map<String, String> retVal = new HashMap<String, String>();
-
- for (Iterator<String> i = resM.keySet().iterator(); i.hasNext(); )
- {
- String iKey = i.next();
- String iVal = resM.get(iKey);
- for (Iterator<String> j = reqM.keySet().iterator(); j.hasNext(); )
- {
- String jKey = j.next();
- String jVal = reqM.get(jKey);
- String jRef = "${" + jKey + "}";
- if (iVal.indexOf(jRef) != -1)
- {
- iVal = StringUtil.replace(jRef, jVal, iVal);
- }
- }
- retVal.put(iKey, iVal);
- }
-
- return retVal;
- }
-
- private View getView(String httpMethod, String req)
- throws ServletException, IOException
- {
- boolean isPost = "POST".equalsIgnoreCase(httpMethod);
- Document requestMessage = isPost ? getDocument(new
ByteArrayInputStream(req.getBytes()), true) : null;
- for (View view : views)
- {
- if (httpMethod.equalsIgnoreCase(view.getRequest().getHttpMethod()))
- {
- if (matches(requestMessage, view))
- {
- return view;
- }
- }
- }
-
- return null;
- }
-
- private static boolean matches(Document req, View view)
- {
- List<String> matches = view.getRequest().getMatches();
- if ((matches == null) || (matches.size() == 0))
- return true;
-
- boolean match = true;
- for (String matchString : matches)
- {
- match = match && elementExists(req, matchString);
- }
-
- return match;
- }
-
- private static boolean elementExists(Document req, String match)
- {
- return getElement(req, match) != null;
- }
-
- private static Element getElement(Document req, String match)
- {
- Element e = null;
-
- StringTokenizer st = new StringTokenizer(match, "|");
- while (st.hasMoreTokens())
- {
- String toConvert = st.nextToken();
- QName nodeName = QName.valueOf(toConvert);
- e = getChildElement(e != null ? e : req, nodeName);
- if (e == null) return null;
- }
-
- return e;
- }
-
- private static Element getChildElement(Node e, QName nodeQName)
- {
- NodeList childNodes = e.getChildNodes();
- if (childNodes == null)
- return null;
-
- for (int i = 0; i < childNodes.getLength(); i++)
- {
- Node node = childNodes.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE)
- {
- Element element = (Element)node;
- boolean namespaceMatches =
nodeQName.getNamespaceURI().equals(element.getNamespaceURI());
- boolean nodeNameMatches =
nodeQName.getLocalPart().equals(element.getLocalName());
- if (namespaceMatches && nodeNameMatches) return element;
- }
- }
-
- return null;
- }
-
- private String modifyResponse(String response, Map<String, String> replaceMap)
- {
- if ((replaceMap != null) && (replaceMap.size() > 0))
- {
- for (Iterator<String> iterator = replaceMap.keySet().iterator();
iterator.hasNext(); )
- {
- String key = iterator.next();
- String val = replaceMap.get(key);
- response = StringUtil.replace("${" + key + "}", val,
response);
- }
- }
- return response;
- }
-
- private String getResource(String resource) throws IOException
- {
- return toString(getServletContext().getResourceAsStream(resource));
- }
-
- private String getRequestMessage(HttpServletRequest req) throws IOException
- {
- BufferedReader reader = req.getReader();
- String line = null;
- StringBuilder sb = new StringBuilder();
- while ((line = reader.readLine()) != null)
- {
- sb.append(line);
- }
- return sb.toString();
- }
-
- private String toString(InputStream is) throws IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int offset = -1;
- while ((offset = is.read(buffer, 0, buffer.length)) != -1)
- {
- baos.write(buffer, 0, offset);
- }
- return baos.toString();
- }
-
}
\ No newline at end of file
Added: stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Util.java
===================================================================
--- stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Util.java
(rev 0)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Util.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -0,0 +1,298 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.test.ws.jaxws.wsrm.emulator;
+
+import static org.jboss.test.ws.jaxws.wsrm.emulator.Constant.*;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.jboss.test.ws.jaxws.wsrm.emulator.config.ObjectFactory;
+import org.jboss.test.ws.jaxws.wsrm.emulator.config.View;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * Utility class
+ *
+ * @author richard.opalka(a)jboss.com
+ *
+ * @since Nov 7, 2007
+ */
+public final class Util
+{
+
+ private Util()
+ {
+ // forbidden inheritance
+ }
+
+ public static String replace(String oldString, String newString, String data)
+ {
+ int fromIndex = 0;
+ int index = 0;
+ StringBuilder result = new StringBuilder();
+
+ while ((index = data.indexOf(oldString, fromIndex)) >= 0)
+ {
+ result.append(data.substring(fromIndex, index));
+ result.append(newString);
+ fromIndex = index + oldString.length();
+ }
+ result.append(data.substring(fromIndex));
+ return result.toString();
+ }
+
+ public static String getResourceAsString(ServletContext ctx, String resource)
+ throws IOException
+ {
+ return toString(ctx.getResourceAsStream(resource));
+ }
+
+ public static Document getDocument(InputStream is, boolean nsAware)
+ throws ServletException
+ {
+ try
+ {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(nsAware);
+ return factory.newDocumentBuilder().parse(is);
+ }
+ catch (SAXException se)
+ {
+ throw new ServletException(se);
+ }
+ catch (ParserConfigurationException pce)
+ {
+ throw new ServletException(pce);
+ }
+ catch (IOException ie)
+ {
+ throw new ServletException(ie);
+ }
+ }
+
+ public static Map<String, String> getNamespaces(Element root)
+ {
+ NodeList namespaces =
((Element)root.getElementsByTagName(NAMESPACES_ELEMENT).item(0)).getElementsByTagName(NAMESPACE_ELEMENT);
+ Map<String, String> retVal = new HashMap<String, String>();
+ for (int i = 0; i < namespaces.getLength(); i++)
+ {
+ Element namespace = (Element)namespaces.item(i);
+ String key = PARAGRAPH + LEFT_BRACKET + namespace.getAttribute(NAME_ATTRIBUTE)
+ RIGHT_BRACKET;
+ retVal.put(key, namespace.getAttribute(VALUE_ATTRIBUTE));
+ }
+ return retVal;
+ }
+
+ public static List<View> getViews(Element root, Map<String, String>
namespaces)
+ {
+ List<View> retVal = new LinkedList<View>();
+ NodeList views = root.getElementsByTagName(VIEW_ELEMENT);
+ for (int i = 0; i < views.getLength(); i++)
+ {
+ retVal.add(ObjectFactory.getView((Element)views.item(i), namespaces));
+ }
+ return retVal;
+ }
+
+ public static Map<String, String> initializeProperties(String reqMsg,
Map<String, String> reqMap)
+ throws ServletException, IOException
+ {
+ Document msgDoc = getDocument(new ByteArrayInputStream(reqMsg.getBytes()), true);
+
+ Map<String, String> retVal = new HashMap<String, String>();
+ for (Iterator<String> i = reqMap.keySet().iterator(); i.hasNext(); )
+ {
+ String key = i.next();
+ String val = reqMap.get(key);
+ Element e = getElement(msgDoc, val);
+ retVal.put(key, e == null ? "" : e.getTextContent());
+ }
+
+ return retVal;
+ }
+
+ public static Map<String, String> prepareReplaceMap(Map<String, String>
reqM, Map<String, String> resM)
+ {
+ Map<String, String> retVal = new HashMap<String, String>();
+
+ for (Iterator<String> i = resM.keySet().iterator(); i.hasNext(); )
+ {
+ String iKey = i.next();
+ String iVal = resM.get(iKey);
+ for (Iterator<String> j = reqM.keySet().iterator(); j.hasNext(); )
+ {
+ String jKey = j.next();
+ String jVal = reqM.get(jKey);
+ String jRef = PARAGRAPH + LEFT_BRACKET + jKey + RIGHT_BRACKET;
+ if (iVal.indexOf(jRef) != -1)
+ {
+ iVal = Util.replace(jRef, jVal, iVal);
+ }
+ }
+ retVal.put(PARAGRAPH + LEFT_BRACKET + iKey + RIGHT_BRACKET, iVal);
+ }
+
+ return retVal;
+ }
+
+ public static View getView(String httpMethod, String req, List<View> views)
+ throws ServletException, IOException
+ {
+ boolean isPost = HTTP_POST.equalsIgnoreCase(httpMethod);
+ Document requestMessage = isPost ? getDocument(new
ByteArrayInputStream(req.getBytes()), true) : null;
+ for (View view : views)
+ {
+ if (httpMethod.equalsIgnoreCase(view.getRequest().getHttpMethod()))
+ {
+ if (matches(requestMessage, view))
+ {
+ return view;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ public static boolean matches(Document req, View view)
+ {
+ List<String> matches = view.getRequest().getMatches();
+ if ((matches == null) || (matches.size() == 0))
+ return true;
+
+ boolean match = true;
+ for (String matchString : matches)
+ {
+ match = match && elementExists(req, matchString);
+ }
+
+ return match;
+ }
+
+ public static boolean elementExists(Document doc, String match)
+ {
+ return getElement(doc, match) != null;
+ }
+
+ public static Element getElement(Document req, String match)
+ {
+ Element e = null;
+
+ StringTokenizer st = new StringTokenizer(match, SEPARATOR);
+ while (st.hasMoreTokens())
+ {
+ String toConvert = st.nextToken();
+ QName nodeName = QName.valueOf(toConvert);
+ e = getChildElement(e != null ? e : req, nodeName);
+ if (e == null) return null;
+ }
+
+ return e;
+ }
+
+ public static Element getChildElement(Node e, QName nodeQName)
+ {
+ NodeList childNodes = e.getChildNodes();
+ if (childNodes == null)
+ return null;
+
+ for (int i = 0; i < childNodes.getLength(); i++)
+ {
+ Node node = childNodes.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ Element element = (Element)node;
+ boolean namespaceMatches =
nodeQName.getNamespaceURI().equals(element.getNamespaceURI());
+ boolean localNameMatches =
nodeQName.getLocalPart().equals(element.getLocalName());
+ if (namespaceMatches && localNameMatches)
+ return element;
+ }
+ }
+
+ return null;
+ }
+
+ public static String replaceAll(String response, Map<String, String>
replaceMap)
+ {
+ if ((replaceMap != null) && (replaceMap.size() > 0))
+ {
+ for (Iterator<String> iterator = replaceMap.keySet().iterator();
iterator.hasNext(); )
+ {
+ String oldString = iterator.next();
+ String newString = replaceMap.get(oldString);
+ response = Util.replace(oldString, newString, response);
+ }
+ }
+ return response;
+ }
+
+ public static String getRequestMessage(HttpServletRequest req)
+ throws IOException
+ {
+ BufferedReader reader = req.getReader();
+ String line = null;
+ StringBuilder sb = new StringBuilder();
+ while ((line = reader.readLine()) != null)
+ {
+ sb.append(line);
+ }
+ return sb.toString();
+ }
+
+ public static String toString(InputStream is)
+ throws IOException
+ {
+ if (is == null)
+ return "";
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+ int offset = -1;
+ while ((offset = is.read(buffer, 0, buffer.length)) != -1)
+ {
+ baos.write(buffer, 0, offset);
+ }
+ return baos.toString();
+ }
+
+}
Property changes on:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/Util.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ObjectFactory.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ObjectFactory.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ObjectFactory.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -26,7 +26,7 @@
import org.w3c.dom.Element;
/**
- * TODO: Add comment
+ * Factory creating configuration objects
*
* @author richard.opalka(a)jboss.com
*
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Request.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Request.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Request.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -25,7 +25,7 @@
import java.util.Map;
/**
- * TODO: Add comment
+ * Interface representing <b>/views/view/request</b> configuration element
content
*
* @author richard.opalka(a)jboss.com
*
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/RequestImpl.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/RequestImpl.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/RequestImpl.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -21,19 +21,20 @@
*/
package org.jboss.test.ws.jaxws.wsrm.emulator.config;
+import static org.jboss.test.ws.jaxws.wsrm.emulator.Constant.*;
+
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import org.jboss.test.ws.jaxws.wsrm.emulator.utils.StringUtil;
+import org.jboss.test.ws.jaxws.wsrm.emulator.Util;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
- * TODO: Add comment
+ * Immutable object implementation representing <b>/views/view/request</b>
configuration element content
*
* @author richard.opalka(a)jboss.com
*
@@ -49,33 +50,34 @@
RequestImpl(Element e, Map<String, String> namespaces)
{
- this.httpMethod = e.getAttribute("httpMethod");
- this.pathInfo = e.getAttribute("pathInfo");
- NodeList contains = e.getElementsByTagName("contains");
+ this.httpMethod = e.getAttribute(HTTP_METHOD_ATTRIBUTE);
+ this.pathInfo = e.getAttribute(PATH_INFO_ATTRIBUTE);
+ NodeList contains = e.getElementsByTagName(CONTAINS_ELEMENT);
if ((contains != null) && (contains.getLength() == 1))
{
- NodeList nodes =
((Element)contains.item(0)).getElementsByTagName("node");
+ NodeList nodes =
((Element)contains.item(0)).getElementsByTagName(NODE_ELEMENT);
List<String> toFill = new LinkedList<String>();
this.matches = Collections.unmodifiableList(toFill);
for (int i = 0; i < nodes.getLength(); i++)
{
- toFill.add(replace(((Element)nodes.item(i)).getAttribute("name"),
namespaces));
+ String nameAttrValue =
((Element)nodes.item(i)).getAttribute(NAME_ATTRIBUTE);
+ toFill.add(Util.replaceAll(nameAttrValue, namespaces));
}
}
else
{
this.matches = Collections.emptyList();
}
- NodeList setNodes = e.getElementsByTagName("set");
+ NodeList setNodes = e.getElementsByTagName(SET_ELEMENT);
if ((setNodes != null) && (setNodes.getLength() > 0))
{
Map<String, String> toFill = new HashMap<String, String>();
this.properties = Collections.unmodifiableMap(toFill);
for (int i = 0; i < setNodes.getLength(); i++)
{
- String key = ((Element)setNodes.item(i)).getAttribute("property");
- String val = ((Element)setNodes.item(i)).getAttribute("value");
- toFill.put(key, replace(val, namespaces));
+ String key = ((Element)setNodes.item(i)).getAttribute(PROPERTY_ATTRIBUTE);
+ String val = ((Element)setNodes.item(i)).getAttribute(VALUE_ATTRIBUTE);
+ toFill.put(key, Util.replaceAll(val, namespaces));
}
}
else
@@ -83,46 +85,35 @@
this.properties = Collections.emptyMap();
}
}
-
- private static String replace(String s, Map<String, String> namespaces)
+
+ public final String getHttpMethod()
{
- for (Iterator<String> i = namespaces.keySet().iterator(); i.hasNext(); )
- {
- String key = i.next();
- String val = namespaces.get(key);
- s = StringUtil.replace("${" + key + "}", val, s);
- }
- return s;
- }
-
- public String getHttpMethod()
- {
return this.httpMethod;
}
- public List<String> getMatches()
+ public final List<String> getMatches()
{
return this.matches;
}
- public String getPathInfo()
+ public final String getPathInfo()
{
return this.pathInfo;
}
- public Map<String, String> getProperties()
+ public final Map<String, String> getProperties()
{
return this.properties;
}
- public String toString()
+ public final String toString()
{
StringBuilder sb = new StringBuilder();
- sb.append("REQUEST {");
- sb.append("httpMethod=" + this.httpMethod + ", ");
- sb.append("pathInfo=" + this.pathInfo + ", ");
- sb.append("properties=" + this.properties + ", ");
- sb.append("matches=" + this.matches + "}");
+ sb.append(REQUEST_ELEMENT).append(EQUAL).append(LEFT_BRACKET);
+
sb.append(HTTP_METHOD_ATTRIBUTE).append(EQUAL).append(this.httpMethod).append(COMMA).append(SPACE);
+
sb.append(PATH_INFO_ATTRIBUTE).append(EQUAL).append(this.pathInfo).append(COMMA).append(SPACE);
+
sb.append(PROPERTIES).append(EQUAL).append(this.properties).append(COMMA).append(SPACE);
+ sb.append(MATCHES).append(EQUAL).append(this.matches).append(RIGHT_BRACKET);
return sb.toString();
}
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Response.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Response.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/Response.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -24,7 +24,7 @@
import java.util.Map;
/**
- * TODO: Add comment
+ * Interface representing <b>/views/view/response</b> configuration element
content
*
* @author richard.opalka(a)jboss.com
*
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ResponseImpl.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ResponseImpl.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ResponseImpl.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -21,17 +21,17 @@
*/
package org.jboss.test.ws.jaxws.wsrm.emulator.config;
+import static org.jboss.test.ws.jaxws.wsrm.emulator.Constant.*;
+
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Map;
-
-import org.jboss.test.ws.jaxws.wsrm.emulator.utils.StringUtil;
+import org.jboss.test.ws.jaxws.wsrm.emulator.Util;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
- * TODO: Add comment
+ * Immutable object implementation representing <b>/views/view/response</b>
configuration element content
*
* @author richard.opalka(a)jboss.com
*
@@ -47,19 +47,19 @@
ResponseImpl(Element e, Map<String, String> namespaces)
{
- this.resource = e.getAttribute("resource");
- this.statusCode = e.getAttribute("statusCode");
- this.contentType = e.getAttribute("contentType");
- NodeList setNodes = e.getElementsByTagName("set");
+ this.resource = e.getAttribute(RESOURCE_ATTRIBUTE);
+ this.statusCode = e.getAttribute(STATUS_CODE_ATTRIBUTE);
+ this.contentType = e.getAttribute(CONTENT_TYPE_ATTRIBUTE);
+ NodeList setNodes = e.getElementsByTagName(SET_ELEMENT);
if ((setNodes != null) && (setNodes.getLength() > 0))
{
Map<String, String> toFill = new HashMap<String, String>();
this.properties = Collections.unmodifiableMap(toFill);
for (int i = 0; i < setNodes.getLength(); i++)
{
- String key = ((Element)setNodes.item(i)).getAttribute("property");
- String val = ((Element)setNodes.item(i)).getAttribute("value");
- toFill.put(key, replace(val, namespaces));
+ String key = ((Element)setNodes.item(i)).getAttribute(PROPERTY_ATTRIBUTE);
+ String val = ((Element)setNodes.item(i)).getAttribute(VALUE_ATTRIBUTE);
+ toFill.put(key, Util.replaceAll(val, namespaces));
}
}
else
@@ -68,45 +68,34 @@
}
}
- private static String replace(String s, Map<String, String> namespaces)
+ public final String getContentType()
{
- for (Iterator<String> i = namespaces.keySet().iterator(); i.hasNext(); )
- {
- String key = i.next();
- String val = namespaces.get(key);
- s = StringUtil.replace("${" + key + "}", val, s);
- }
- return s;
- }
-
- public String getContentType()
- {
return this.contentType;
}
- public Map<String, String> getProperties()
+ public final Map<String, String> getProperties()
{
return this.properties;
}
- public String getResource()
+ public final String getResource()
{
return this.resource;
}
- public String getStatusCode()
+ public final String getStatusCode()
{
return this.statusCode;
}
- public String toString()
+ public final String toString()
{
StringBuilder sb = new StringBuilder();
- sb.append("RESPONSE {");
- sb.append("resource=" + this.resource + ", ");
- sb.append("statusCode=" + this.statusCode + ", ");
- sb.append("contentType=" + this.contentType + ", ");
- sb.append("properties=" + this.properties + "}");
+ sb.append(RESPONSE_ELEMENT).append(EQUAL).append(LEFT_BRACKET);
+
sb.append(RESOURCE_ATTRIBUTE).append(EQUAL).append(this.resource).append(COMMA).append(SPACE);
+
sb.append(STATUS_CODE_ATTRIBUTE).append(EQUAL).append(this.statusCode).append(COMMA).append(SPACE);
+
sb.append(CONTENT_TYPE_ATTRIBUTE).append(EQUAL).append(this.contentType).append(COMMA).append(SPACE);
+ sb.append(PROPERTIES).append(EQUAL).append(this.properties).append(RIGHT_BRACKET);
return sb.toString();
}
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/View.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/View.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/View.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -22,7 +22,7 @@
package org.jboss.test.ws.jaxws.wsrm.emulator.config;
/**
- * TODO: Add comment
+ * Interface representing <b>/views/view</b> configuration element content
*
* @author richard.opalka(a)jboss.com
*
Modified:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ViewImpl.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ViewImpl.java 2007-11-09
11:22:13 UTC (rev 5023)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/wsrm/emulator/config/ViewImpl.java 2007-11-09
16:26:48 UTC (rev 5024)
@@ -21,13 +21,14 @@
*/
package org.jboss.test.ws.jaxws.wsrm.emulator.config;
+import static org.jboss.test.ws.jaxws.wsrm.emulator.Constant.*;
+
import java.util.Map;
-
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
- * TODO: Add comment
+ * Immutable object implementation representing <b>/views/view</b>
configuration element content
*
* @author richard.opalka(a)jboss.com
*
@@ -42,35 +43,35 @@
ViewImpl(Element e, Map<String, String> namespaces)
{
- this.id = e.getAttribute("id");
- NodeList response = e.getElementsByTagName("response");
- this.res = (response == null) ? null :
ObjectFactory.getResponse((Element)response.item(0), namespaces);
- NodeList request = e.getElementsByTagName("request");
- this.req = (request == null) ? null :
ObjectFactory.getRequest((Element)request.item(0), namespaces);
+ this.id = e.getAttribute(ID_ATTRIBUTE);
+ NodeList response = e.getElementsByTagName(RESPONSE_ELEMENT);
+ this.res = ObjectFactory.getResponse((Element)response.item(0), namespaces);
+ NodeList request = e.getElementsByTagName(REQUEST_ELEMENT);
+ this.req = ObjectFactory.getRequest((Element)request.item(0), namespaces);
}
- public String getId()
+ public final String getId()
{
return this.id;
}
- public Request getRequest()
+ public final Request getRequest()
{
return this.req;
}
- public Response getResponse()
+ public final Response getResponse()
{
return this.res;
}
- public String toString()
+ public final String toString()
{
StringBuilder sb = new StringBuilder();
- sb.append("VIEW {");
- sb.append("id=" + id + ", ");
- sb.append("request=" + req + ", ");
- sb.append("response=" + res + "}");
+ sb.append(VIEW_ELEMENT).append(EQUAL).append(LEFT_BRACKET);
+ sb.append(ID_ATTRIBUTE).append(EQUAL).append(id).append(COMMA).append(SPACE);
+ sb.append(REQUEST_ELEMENT).append(EQUAL).append(req).append(COMMA).append(SPACE);
+ sb.append(RESPONSE_ELEMENT).append(EQUAL).append(res).append(RIGHT_BRACKET);
return sb.toString();
}