Source Code Cross Referenced for EchoAttachmentsService.java in  » J2EE » enhydra » samples » attachments » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » enhydra » samples.attachments 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package samples.attachments;
018:
019:        import org.w3c.dom.Document;
020:        import org.w3c.dom.Element;
021:        import org.w3c.dom.Node;
022:
023:        import javax.activation.DataHandler;
024:
025:        /**
026:         * @author Rick Rineholt 
027:         */
028:
029:        /**
030:         * An example of 
031:         * This class has a main method that beside the standard arguments
032:         * allows you to specify an attachment that will be sent to a 
033:         * service which will then send it back.
034:         *  
035:         */
036:        public class EchoAttachmentsService {
037:
038:            /**
039:             * This method implements a web service that sends back
040:             * any attachment it receives.
041:             */
042:            public DataHandler echo(DataHandler dh) {
043:                System.err.println("In echo");
044:
045:                //Attachments are sent by default back as a MIME stream if no attachments were
046:                // received.  If attachments are received the same format that was received will
047:                // be the default stream type for any attachments sent.
048:
049:                //The following two commented lines would force any attachments sent back.
050:                //  to be in DIME format.
051:
052:                //Message rspmsg=AxisEngine.getCurrentMessageContext().getResponseMessage();
053:                //rspmsg.getAttachmentsImpl().setSendType(org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
054:
055:                if (dh == null)
056:                    System.err.println("dh is null");
057:                else
058:                    System.err.println("Received \"" + dh.getClass().getName()
059:                            + "\".");
060:                return dh;
061:            }
062:
063:            /**
064:             * This method implements a web service that sends back
065:             * an array of attachment it receives.
066:             */
067:            public DataHandler[] echoDir(DataHandler[] attachments) {
068:                System.err.println("In echoDir");
069:
070:                //Attachments are sent by default back as a MIME stream if no attachments were
071:                // received.  If attachments are received the same format that was received will
072:                // be the default stream type for any attachments sent.
073:
074:                //The following two commented lines would force any attachments sent back.
075:                //  to be in DIME format.
076:
077:                //Message rspmsg=AxisEngine.getCurrentMessageContext().getResponseMessage();
078:                //rspmsg.getAttachmentsImpl().setSendType(org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
079:
080:                if (attachments == null)
081:                    System.err.println("attachments is null!");
082:                else
083:                    System.err.println("Got " + attachments.length
084:                            + " attachments!");
085:                return attachments;
086:            }
087:
088:            public Document attachments(Document xml)
089:                    throws org.apache.axis.AxisFault, java.io.IOException,
090:                    org.xml.sax.SAXException,
091:                    java.awt.datatransfer.UnsupportedFlavorException,
092:                    javax.xml.parsers.ParserConfigurationException,
093:                    java.lang.ClassNotFoundException,
094:                    javax.xml.soap.SOAPException {
095:                System.err.println("In message handling attachments directly.");
096:                org.apache.axis.MessageContext msgContext = org.apache.axis.MessageContext
097:                        .getCurrentContext();
098:
099:                org.apache.axis.Message reqMsg = msgContext.getRequestMessage();
100:
101:                org.apache.axis.attachments.Attachments attachments = reqMsg
102:                        .getAttachmentsImpl();
103:
104:                if (null == attachments) {
105:                    throw new org.apache.axis.AxisFault(
106:                            "No support for attachments");
107:                }
108:
109:                Element rootEl = xml.getDocumentElement();
110:
111:                Element caEl = getNextFirstChildElement(rootEl);
112:                StringBuffer fullmsg = new StringBuffer();
113:                java.util.Vector reply = new java.util.Vector();
114:
115:                for (int count = 1; caEl != null; caEl = getNextSiblingElement(caEl), ++count) {
116:                    String href = caEl.getAttribute("href");
117:                    org.apache.axis.Part p = attachments
118:                            .getAttachmentByReference(href);
119:                    if (null == p)
120:                        throw new org.apache.axis.AxisFault(
121:                                "Attachment for ref='" + href + "' not found.");
122:                    String ordinalStr = getOrdinalHeaders(p);
123:                    if (null == ordinalStr || ordinalStr.trim().length() == 0)
124:                        throw new org.apache.axis.AxisFault(
125:                                "Ordinal for attachment  ref='" + href
126:                                        + "' not found.");
127:                    int ordinal = Integer.parseInt(ordinalStr);
128:                    if (count != ordinal)
129:                        throw new org.apache.axis.AxisFault(
130:                                "Ordinal for attachment  ref='" + href
131:                                        + "' excpected" + count + " got "
132:                                        + ordinal + ".");
133:
134:                    //check content type.
135:                    if (!"text/plain".equals(p.getContentType()))
136:                        throw new org.apache.axis.AxisFault("Attachment  ref='"
137:                                + href + "' bad content-type:'"
138:                                + p.getContentType() + "'.");
139:
140:                    //now get at the data...
141:                    DataHandler dh = ((org.apache.axis.attachments.AttachmentPart) p)
142:                            .getDataHandler();
143:                    String pmsg = (String) dh.getContent();
144:                    fullmsg.append(pmsg);
145:                    reply.add(pmsg);
146:                }
147:                if (!(samples.attachments.TestRef.TheKey.equals(fullmsg
148:                        .toString())))
149:                    throw new org.apache.axis.AxisFault("Fullmsg not correct'"
150:                            + fullmsg + "'.");
151:                System.out.println(fullmsg.toString());
152:
153:                //Now lets Java serialize the reply...
154:                java.io.ByteArrayOutputStream byteStream = new java.io.ByteArrayOutputStream();
155:                java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(
156:                        byteStream);
157:                oos.writeObject(reply);
158:                oos.close();
159:                byte[] replyJavaSerialized = byteStream.toByteArray();
160:                byteStream = null;
161:                oos = null;
162:
163:                org.apache.axis.attachments.AttachmentPart replyPart = new org.apache.axis.attachments.AttachmentPart(
164:                        new DataHandler(
165:                                new MemoryOnlyDataSource(
166:                                        replyJavaSerialized,
167:                                        java.awt.datatransfer.DataFlavor.javaSerializedObjectMimeType
168:                                                + "; class=\""
169:                                                + reply.getClass().getName()
170:                                                + "\"")));
171:
172:                //Now lets add the attachment to the response message.
173:                org.apache.axis.Message rspMsg = msgContext
174:                        .getResponseMessage();
175:                rspMsg.addAttachmentPart(replyPart);
176:
177:                //Iterate over the attachments... not by reference.
178:                String ordinalPattern = "";
179:                for (java.util.Iterator ai = reqMsg.getAttachments(); ai
180:                        .hasNext();) {
181:                    org.apache.axis.Part p = (org.apache.axis.Part) ai.next();
182:                    ordinalPattern += getOrdinalHeaders(p);
183:                }
184:
185:                //Now build the return document in a string buffer... 
186:                StringBuffer msgBody = new StringBuffer(
187:                        "\n<attachments xmlns=\"");
188:                msgBody.append(rootEl.getNamespaceURI()).append("\">\n")
189:                        .append("\t<attachment href=\"").append(
190:                                replyPart.getContentIdRef()).append(
191:                                "\" ordinalPattern=\"").append(ordinalPattern)
192:                        .append("\"/>\n").append("</attachments>\n");
193:
194:                //Convert the string buffer to an XML document and return it.
195:                return org.apache.axis.utils.XMLUtils
196:                        .newDocument(new org.xml.sax.InputSource(
197:                                new java.io.ByteArrayInputStream(msgBody
198:                                        .toString().getBytes())));
199:            }
200:
201:            Element getNextFirstChildElement(Node n) {
202:                if (n == null)
203:                    return null;
204:                n = n.getFirstChild();
205:                for (; n != null && !(n instanceof  Element); n = n
206:                        .getNextSibling())
207:                    ;
208:                return (Element) n;
209:            }
210:
211:            Element getNextSiblingElement(Node n) {
212:                if (n == null)
213:                    return null;
214:                n = n.getNextSibling();
215:                for (; n != null && !(n instanceof  Element); n = n
216:                        .getNextSibling())
217:                    ;
218:                return (Element) n;
219:            }
220:
221:            String getOrdinalHeaders(org.apache.axis.Part p) {
222:                StringBuffer ret = new StringBuffer();
223:                for (java.util.Iterator i = p
224:                        .getMatchingMimeHeaders(new String[] { samples.attachments.TestRef.positionHTTPHeader }); i
225:                        .hasNext();) {
226:                    javax.xml.soap.MimeHeader mh = (javax.xml.soap.MimeHeader) i
227:                            .next();
228:                    String v = mh.getValue();
229:                    if (v != null)
230:                        ret.append(v.trim());
231:                }
232:                return ret.toString();
233:            }
234:
235:            /**This class should store all attachment data in memory */
236:            static class MemoryOnlyDataSource extends
237:                    org.apache.axis.attachments.ManagedMemoryDataSource {
238:
239:                MemoryOnlyDataSource(byte[] in, String contentType)
240:                        throws java.io.IOException {
241:                    super (new java.io.ByteArrayInputStream(in),
242:                            Integer.MAX_VALUE - 2, contentType, true);
243:                }
244:
245:                MemoryOnlyDataSource(String in, String contentType)
246:                        throws java.io.IOException {
247:                    this(in.getBytes(), contentType);
248:                }
249:            }
250:
251:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.