001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.shipment.thirdparty.usps;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.servlet.ServletConfig;
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletOutputStream;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.xml.parsers.ParserConfigurationException;
033:
034: import org.ofbiz.base.util.UtilValidate;
035: import org.ofbiz.base.util.UtilXml;
036: import org.ofbiz.base.util.Debug;
037:
038: import org.apache.xml.serialize.OutputFormat;
039: import org.apache.xml.serialize.XMLSerializer;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.xml.sax.SAXException;
043:
044: /**
045: * USPS Webtools API Mock API Servlet
046: */
047: public class UspsMockApiServlet extends HttpServlet {
048:
049: public static final String module = UspsMockApiServlet.class
050: .getName();
051:
052: public UspsMockApiServlet() {
053: super ();
054: }
055:
056: public void init(ServletConfig config) throws ServletException {
057: super .init(config);
058: }
059:
060: public void doPost(HttpServletRequest request,
061: HttpServletResponse response) throws ServletException,
062: IOException {
063: doGet(request, response);
064: }
065:
066: public void doGet(HttpServletRequest request,
067: HttpServletResponse response) throws ServletException,
068: IOException {
069:
070: // we're only testing the Rate API right now
071: if (!"Rate".equals(request.getParameter("API"))) {
072: Debug.logError("Unsupported API ["
073: + request.getParameter("API") + "]", module);
074: return;
075: }
076:
077: String xmlValue = request.getParameter("XML");
078: Document requestDocument = null;
079: try {
080: requestDocument = UtilXml.readXmlDocument(xmlValue, false);
081: } catch (SAXException se) {
082: Debug.logError(se, module);
083: return;
084: } catch (ParserConfigurationException pce) {
085: Debug.logError(pce, module);
086: return;
087: } catch (IOException xmlReadException) {
088: Debug.logError(xmlReadException, module);
089: return;
090: }
091:
092: if (requestDocument == null) {
093: Debug
094: .logError(
095: "In UspsMockApiSerlvet No XML document found in request, quiting now; XML parameter is: "
096: + xmlValue, module);
097: return;
098: }
099:
100: List packageElementList = UtilXml.childElementList(
101: requestDocument.getDocumentElement(), "Package");
102: if (UtilValidate.isNotEmpty(packageElementList)) {
103:
104: Document responseDocument = UtilXml
105: .makeEmptyXmlDocument("RateResponse");
106: for (Iterator i = packageElementList.iterator(); i
107: .hasNext();) {
108: Element packageElement = (Element) i.next();
109:
110: Element responsePackageElement = UtilXml
111: .addChildElement(responseDocument
112: .getDocumentElement(), "Package",
113: responseDocument);
114: responsePackageElement.setAttribute("ID",
115: packageElement.getAttribute("ID"));
116:
117: UtilXml.addChildElementValue(responsePackageElement,
118: "ZipOrigination", UtilXml.childElementValue(
119: packageElement, "ZipOrigination"),
120: responseDocument);
121:
122: UtilXml.addChildElementValue(responsePackageElement,
123: "ZipDestination", UtilXml.childElementValue(
124: packageElement, "ZipDestination"),
125: responseDocument);
126:
127: UtilXml.addChildElementValue(responsePackageElement,
128: "Pounds", UtilXml.childElementValue(
129: packageElement, "Pounds"),
130: responseDocument);
131:
132: UtilXml.addChildElementValue(responsePackageElement,
133: "Ounces", UtilXml.childElementValue(
134: packageElement, "Ounces"),
135: responseDocument);
136:
137: UtilXml.addChildElementValue(responsePackageElement,
138: "Container", UtilXml.childElementValue(
139: packageElement, "Container"),
140: responseDocument);
141:
142: UtilXml.addChildElementValue(responsePackageElement,
143: "Size", UtilXml.childElementValue(
144: packageElement, "Size"),
145: responseDocument);
146:
147: UtilXml.addChildElementValue(responsePackageElement,
148: "Zone", "1", responseDocument);
149: UtilXml.addChildElementValue(responsePackageElement,
150: "Postage", "3.00", responseDocument);
151: }
152:
153: OutputStream os = new ByteArrayOutputStream();
154:
155: OutputFormat format = new OutputFormat(responseDocument);
156: format.setOmitDocumentType(true);
157: format.setOmitXMLDeclaration(false);
158: format.setIndenting(false);
159:
160: XMLSerializer serializer = new XMLSerializer(os, format);
161: try {
162: serializer.asDOMSerializer();
163: serializer.serialize(responseDocument
164: .getDocumentElement());
165: } catch (IOException e) {
166: Debug.log(e, module);
167: return;
168: }
169:
170: response.setContentType("text/xml");
171: ServletOutputStream sos = response.getOutputStream();
172: sos.print(os.toString());
173: sos.flush();
174: }
175: }
176:
177: public void destroy() {
178: super.destroy();
179: }
180: }
|