| 1 5 6 package ebay.server; 7 8 import java.io.StringReader ; 9 import javax.xml.ws.WebServiceContext; 10 import java.io.ByteArrayInputStream ; 11 import javax.xml.ws.Provider; 12 import javax.xml.transform.Source ; 13 import javax.xml.transform.stream.StreamSource ; 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 ; 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Properties ; 25 import javax.xml.ws.WebServiceException; 26 27 import javax.xml.transform.OutputKeys ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.stream.StreamResult ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.InputStream ; 33 import javax.activation.DataHandler ; 34 import javax.jws.HandlerChain; 35 import javax.activation.DataSource ; 36 import java.io.OutputStream ; 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 > { 44 45 @Resource 46 protected WebServiceContext wsContext; 47 48 public DataSource invoke(DataSource ds) { 49 MessageContext mc = wsContext.getMessageContext(); 50 String method = (String )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 get(DataSource source, MessageContext mc) { 60 final String image; 61 String path = (String )mc.get(MessageContext.PATH_INFO); 62 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 () { 74 public InputStream getInputStream() { 75 return this.getClass().getClassLoader(). 76 getResourceAsStream( 77 image); 78 } 79 public OutputStream getOutputStream() { 80 return null; 81 } 82 public String getContentType() { 83 return "image/jpeg"; 84 } 85 public String getName() { 86 return ""; 87 } 88 }; 89 } 90 } 91 | Popular Tags |