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: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: package book.sender;
057:
058: import java.io.*;
059: import java.net.URL;
060: import java.util.Properties;
061: import java.util.logging.Level;
062: import java.util.logging.Logger;
063:
064: import javax.activation.DataHandler;
065: import javax.servlet.*;
066: import javax.servlet.http.*;
067: import javax.xml.parsers.DocumentBuilder;
068: import javax.xml.parsers.DocumentBuilderFactory;
069: import javax.xml.soap.*;
070:
071: import org.w3c.dom.Document;
072:
073: /**
074: * Sample servlet that is used for sending the message.
075: *
076: * @author Krishna Meduri (krishna.meduri@sun.com)
077: */
078:
079: public class SendingServlet extends HttpServlet {
080:
081: String to = null;
082: String data = null;
083: ServletContext servletContext;
084:
085: private static Logger logger = Logger.getLogger("Samples/Book");
086:
087: // Connection to send messages.
088: private SOAPConnection con;
089:
090: public void init(ServletConfig servletConfig)
091: throws ServletException {
092: super .init(servletConfig);
093: servletContext = servletConfig.getServletContext();
094:
095: try {
096: SOAPConnectionFactory scf = SOAPConnectionFactory
097: .newInstance();
098: con = scf.createConnection();
099: } catch (Exception e) {
100: logger.log(Level.SEVERE, "Unable to open a SOAPConnection",
101: e);
102: }
103:
104: InputStream in = servletContext
105: .getResourceAsStream("/WEB-INF/address.properties");
106:
107: if (in != null) {
108: Properties props = new Properties();
109:
110: try {
111: props.load(in);
112:
113: to = props.getProperty("to");
114: data = props.getProperty("data");
115: } catch (IOException ex) {
116: // Ignore
117: }
118: }
119: }
120:
121: public void doGet(HttpServletRequest req, HttpServletResponse resp)
122: throws ServletException {
123:
124: String retval = "<html> <H4>";
125:
126: try {
127: // Create a message factory.
128: MessageFactory mf = MessageFactory.newInstance();
129:
130: // Create a message from the message factory.
131: SOAPMessage msg = mf.createMessage();
132:
133: // Message creation takes care of creating the SOAPPart - a
134: // required part of the message as per the SOAP 1.1
135: // specification.
136: SOAPPart sp = msg.getSOAPPart();
137:
138: // Retrieve the envelope from the soap part to start building
139: // the soap message.
140: SOAPEnvelope envelope = sp.getEnvelope();
141:
142: // Get the soap SOAP Header from the message
143: SOAPHeader header = msg.getSOAPHeader();
144:
145: Name book = envelope.createName("book", "b",
146: "http://saaj.sample/book");
147: SOAPHeaderElement bookHeaderElem = header
148: .addHeaderElement(book);
149: bookHeaderElem.setActor("http://saaj.sample/receiver");
150: bookHeaderElem.setMustUnderstand(true);
151:
152: Name isbn = envelope.createName("isbn", "b",
153: "http://saaj.sample/book");
154: SOAPElement isbnElem = bookHeaderElem.addChildElement(isbn);
155: isbnElem.addTextNode("9-999-99999-9");
156:
157: SOAPFactory soapFactory = SOAPFactory.newInstance();
158: Name edition = soapFactory.createName("edition", "b",
159: "http://saaj.sample/book");
160: SOAPElement editionElem = bookHeaderElem
161: .addChildElement(edition);
162: editionElem.addTextNode("2");
163:
164: // Get the soap SOAP Body from the message
165: SOAPBody body = msg.getSOAPBody();
166:
167: StringBuffer urlSB = new StringBuffer("http://");
168: urlSB.append(req.getServerName());
169: urlSB.append(":").append(req.getServerPort());
170: urlSB.append(req.getContextPath());
171: String reqBase = urlSB.toString();
172:
173: DocumentBuilderFactory dbf = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
174: dbf.setNamespaceAware(true);
175: DocumentBuilder docBuilder = dbf.newDocumentBuilder();
176: Document bookInfo = docBuilder.parse(reqBase
177: + "/bookinfo.xml");
178:
179: body.addDocument(bookInfo);
180:
181: Name author = envelope.createName("author", "a",
182: "http://saaj.sample/author");
183: SOAPBodyElement authorElem = body.addBodyElement(author);
184: SOAPElement authorName = authorElem.addChildElement("name",
185: "a", "http://saaj.sample/author");
186: authorName.addTextNode("John Rhodes");
187:
188: if (data == null) {
189: data = reqBase + "/index.html";
190: }
191:
192: // Want to set an attachment from the following url.
193: //Get context
194: URL url = new URL(data);
195:
196: AttachmentPart ap = msg
197: .createAttachmentPart(new DataHandler(url));
198:
199: ap.setContentType("text/html");
200:
201: // Add the attachment part to the message.
202: msg.addAttachmentPart(ap);
203:
204: // Create an endpoint for the recipient of the message.
205: if (to == null) {
206: to = reqBase + "/receiver";
207: }
208:
209: URL urlEndpoint = new URL(to);
210:
211: System.err
212: .println("Sending message to URL: " + urlEndpoint);
213: System.err
214: .println("Sent message is logged in \"sent.msg\"");
215:
216: retval += " Sent message (check \"sent.msg\") and ";
217:
218: FileOutputStream sentFile = new FileOutputStream("sent.msg");
219: msg.writeTo(sentFile);
220: sentFile.close();
221:
222: // Send the message to the provider using the connection.
223: SOAPMessage reply = con.call(msg, urlEndpoint);
224:
225: if (reply != null) {
226: FileOutputStream replyFile = new FileOutputStream(
227: "reply.msg");
228: reply.writeTo(replyFile);
229: replyFile.close();
230: System.err.println("Reply logged in \"reply.msg\"");
231: retval += " received reply (check \"reply.msg\").</H4> </html>";
232:
233: } else {
234: System.err.println("No reply");
235: retval += " no reply was received. </H4> </html>";
236: }
237:
238: } catch (Throwable e) {
239: e.printStackTrace();
240: logger.severe("Error in constructing or sending message "
241: + e.getMessage());
242: retval += " There was an error "
243: + "in constructing or sending message. </H4> </html>";
244: }
245:
246: try {
247: OutputStream os = resp.getOutputStream();
248: os.write(retval.getBytes());
249: os.flush();
250: os.close();
251: } catch (IOException e) {
252: e.printStackTrace();
253: logger.severe("Error in outputting servlet response "
254: + e.getMessage());
255: }
256: }
257:
258: }
|