KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ebay > server > ImageProvider


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package ebay.server;
7
8 import java.io.StringReader JavaDoc;
9 import javax.xml.ws.WebServiceContext;
10 import java.io.ByteArrayInputStream JavaDoc;
11 import javax.xml.ws.Provider;
12 import javax.xml.transform.Source JavaDoc;
13 import javax.xml.transform.stream.StreamSource JavaDoc;
14 import javax.xml.ws.WebServiceProvider;
15 import javax.xml.ws.Service;
16 import javax.xml.ws.ServiceMode;
17 import javax.xml.ws.handler.MessageContext;
18 import javax.annotation.Resource;
19 import javax.xml.ws.http.HTTPException;
20 import java.util.Collections JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.xml.ws.WebServiceException;
26
27 import javax.xml.transform.OutputKeys JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import javax.activation.DataHandler JavaDoc;
34 import javax.jws.HandlerChain;
35 import javax.activation.DataSource JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import javax.xml.ws.http.HTTPBinding;
38 import javax.xml.ws.BindingType;
39
40 @WebServiceProvider
41 @BindingType(HTTPBinding.HTTP_BINDING)
42 @ServiceMode(value=Service.Mode.MESSAGE)
43 public class ImageProvider implements Provider<DataSource JavaDoc> {
44
45     @Resource
46     protected WebServiceContext wsContext;
47
48     public DataSource JavaDoc invoke(DataSource JavaDoc ds) {
49         MessageContext mc = wsContext.getMessageContext();
50         String JavaDoc method = (String JavaDoc)mc.get(MessageContext.HTTP_REQUEST_METHOD);
51         if (method.equals("GET")) {
52             return get(ds, mc);
53         }
54         HTTPException ex = new HTTPException(404);
55         throw ex;
56     }
57
58   
59     private DataSource JavaDoc get(DataSource JavaDoc source, MessageContext mc) {
60         final String JavaDoc image;
61         String JavaDoc path = (String JavaDoc)mc.get(MessageContext.PATH_INFO);
62 // System.out.println("PathInfo = "+path);
63
if (path != null && path.equals("/sun_blade_1000_h400px.jpg")) {
64             image = "images/sun_blade_1000_h400px.jpg";
65         }else if(path != null && path.equals("/Sun_Fire_E20K_Server.jpg")) {
66             image = "images/Sun_Fire_E20K_Server.jpg";
67         } else if (path != null && path.equals("/8d_2.JPG")) {
68             image = "images/8d_2.JPG";
69         } else {
70             HTTPException ex = new HTTPException(404);
71             throw ex;
72         }
73             return new DataSource JavaDoc() {
74                                 public InputStream JavaDoc getInputStream() {
75                                         return this.getClass().getClassLoader().
76 getResourceAsStream(
77                                                 image);
78                                 }
79                                 public OutputStream JavaDoc getOutputStream() {
80                                         return null;
81                                 }
82                                 public String JavaDoc getContentType() {
83                                         return "image/jpeg";
84                                 }
85                                 public String JavaDoc getName() {
86                                         return "";
87                                 }
88                         };
89         }
90 }
91
Popular Tags