001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: SendingServlet.java,v 1.2 2007/07/16 16:41:18 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:18 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package translator;
062:
063: import java.io.FileOutputStream;
064: import java.io.PrintWriter;
065: import java.net.URL;
066: import java.util.Iterator;
067: import java.util.logging.Level;
068: import java.util.logging.Logger;
069:
070: import javax.servlet.ServletConfig;
071: import javax.servlet.ServletException;
072: import javax.servlet.http.*;
073: import javax.xml.soap.*;
074:
075: /**
076: * Sample servlet that is used for sending the message.
077: *
078: * @author Manveen Kaur (manveen.kaur@sun.com)
079: *
080: */
081: public class SendingServlet extends HttpServlet {
082:
083: private static final String NS_PREFIX = "saaj";
084: private static final String NS_URI = "http://java.sun.com/saaj/samples/translation";
085: private static Logger logger = Logger
086: .getLogger("Samples/Translator");
087:
088: // the translations in different languages.
089: private String french = "";
090: private String italian = "";
091: private String german = "";
092:
093: SOAPConnection conn;
094:
095: public void init(ServletConfig servletConfig)
096: throws ServletException {
097: super .init(servletConfig);
098: }
099:
100: public void doGet(HttpServletRequest req, HttpServletResponse resp)
101: throws ServletException {
102:
103: try {
104: SOAPConnectionFactory scf = SOAPConnectionFactory
105: .newInstance();
106: conn = scf.createConnection();
107: } catch (Exception e) {
108: logger.log(Level.SEVERE, "Unable to open a SOAPConnection",
109: e);
110: }
111:
112: try {
113:
114: // Extracting the text to be translated from the HTML form.
115: String text = req.getParameter("translate");
116:
117: //Extracting proxy settings.
118: String host = req.getParameter("host");
119: String port = req.getParameter("port");
120: String trAs = req.getParameter("trAs");
121:
122: if ((text == null) || (text.equals(""))) {
123: resp.setContentType("text/html");
124: PrintWriter out = resp.getWriter();
125: out.println("<HTML><BODY BGCOLOR=\"pink\">\n");
126: out.println("<H4>No text was entered.");
127: out.println("Please try again.</H4>\n</BODY></HTML>");
128:
129: } else {
130:
131: MessageFactory mf = MessageFactory.newInstance();
132: SOAPMessage msg = mf.createMessage();
133:
134: SOAPPart sp = msg.getSOAPPart();
135: SOAPEnvelope envelope = sp.getEnvelope();
136:
137: SOAPHeader header = envelope.getHeader();
138: SOAPBody body = envelope.getBody();
139:
140: header.addHeaderElement(
141: envelope.createName("ProxyHost", NS_PREFIX,
142: NS_URI)).addTextNode(host);
143:
144: header.addHeaderElement(
145: envelope.createName("ProxyPort", NS_PREFIX,
146: NS_URI)).addTextNode(port);
147:
148: header.addHeaderElement(
149: envelope.createName("TranslationAs", NS_PREFIX,
150: NS_URI)).addTextNode(trAs);
151:
152: body.addBodyElement(
153: envelope.createName("Text", NS_PREFIX, NS_URI))
154: .addTextNode(text);
155:
156: // Sending message.
157: StringBuffer urlSB = new StringBuffer();
158: urlSB.append(req.getScheme()).append("://").append(
159: req.getServerName());
160: urlSB.append(":").append(req.getServerPort()).append(
161: req.getContextPath());
162: String reqBase = urlSB.toString();
163:
164: URL endPoint = new URL(reqBase + "/translationservice");
165:
166: SOAPMessage reply = conn.call(msg, endPoint);
167:
168: System.out
169: .println("\n************** REPLY ***************\n");
170: reply.writeTo(System.out);
171: FileOutputStream os = new FileOutputStream("reply.msg");
172: reply.writeTo(os);
173: os.close();
174:
175: if (trAs.equals("body")) {
176: extractFromBody(reply);
177: } else {
178: extractFromAttachments(reply);
179: }
180:
181: // Printing out the translated text.
182: resp.setContentType("text/html");
183: PrintWriter out = resp.getWriter();
184: out
185: .println("<HTML>\n"
186: + "<BODY BGCOLOR=\"pink\">\n"
187: + "Translation of <FONT COLOR=\"black\"> <B>"
188: + text
189: + "</B></FONT> in different languages:<P>\n"
190: + "<TABLE BORDER=1 CELLPADDING=10 CELLSPACING=2>\n"
191: + "<TR BGCOLOR=\"white\">\n"
192: + "<TD> French </TD>\n"
193: + "<TD> German </TD>\n"
194: + "<TD> Italian </TD>\n"
195: + "</TR> \n <TR>" + "<TD>" + french
196: + "</TD>\n" + "<TD>" + german
197: + " </TD>\n" + "<TD>" + italian
198: + "</TD>\n" + "\n </TR> \n"
199: + "</TABLE> ");
200:
201: out
202: .println("<BR> Request logged in file <I> request.msg </I>"
203: + "\n Reply logged in file <I> reply.msg </I> "
204: + "\n</BODY> \n</HTML>");
205:
206: conn.close();
207: }
208: } catch (Exception e) {
209: e.printStackTrace();
210: logger.severe("Error in processing reply message "
211: + e.getMessage());
212: }
213: }
214:
215: private void extractFromBody(SOAPMessage reply) {
216: try {
217:
218: SOAPEnvelope envelope = reply.getSOAPPart().getEnvelope();
219: SOAPBody body = envelope.getBody();
220:
221: french = extract(envelope, body, "FrenchText");
222: german = extract(envelope, body, "GermanText");
223: italian = extract(envelope, body, "ItalianText");
224:
225: } catch (Exception e) {
226: e.printStackTrace();
227: logger.severe("Error in extracting text from the body");
228: }
229: }
230:
231: private void extractFromAttachments(SOAPMessage reply) {
232: try {
233: // Extracting the content from the message attachments.
234: Iterator iterator = reply.getAttachments();
235:
236: if (iterator.hasNext())
237: french = (String) ((AttachmentPart) iterator.next())
238: .getContent();
239:
240: if (iterator.hasNext())
241: german = (String) ((AttachmentPart) iterator.next())
242: .getContent();
243:
244: if (iterator.hasNext())
245: italian = (String) ((AttachmentPart) iterator.next())
246: .getContent();
247:
248: } catch (Exception e) {
249: logger.severe("Error in extracting text from attachments "
250: + e.getMessage());
251: }
252: }
253:
254: // extract the value of the first child element under element
255: // with this localname
256: private String extract(SOAPEnvelope envelope, SOAPElement element,
257: String localname) throws SOAPException {
258:
259: Iterator it = element.getChildElements(envelope.createName(
260: localname, NS_PREFIX, NS_URI));
261:
262: if (it.hasNext()) {
263: SOAPElement e = (SOAPElement) it.next();
264: return e.getValue();
265: }
266: logger.severe("Could not extract " + localname
267: + " from message");
268: return null;
269: }
270:
271: }
|