Created
January 20, 2010 00:18
-
-
Save jamescarr/281463 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jws.WebMethod; | |
import javax.jws.WebParam; | |
import javax.jws.WebService; | |
@WebService | |
public interface GeographicalService { | |
@WebMethod | |
GeographicalInformation lookupGeography(@WebParam GeographicalRequest type); | |
} | |
--------------------------------------- | |
import java.io.InputStream; | |
import javax.activation.DataHandler; | |
import javax.activation.DataSource; | |
import javax.xml.ws.Endpoint; | |
import javax.xml.ws.soap.SOAPBinding; | |
import org.apache.axiom.attachments.ByteArrayDataSource; | |
import org.apache.commons.io.IOUtils; | |
public class EndpointPublisher implements GeographicalService { | |
public static void main(String... args){ | |
Endpoint ep = Endpoint.publish("http://localhost/myService", new EndpointPublisher()); | |
SOAPBinding binding = (SOAPBinding) ep.getBinding(); | |
binding.setMTOMEnabled(true); | |
} | |
@Override | |
public GeographicalInformation lookupGeography(GeographicalRequest type) { | |
System.out.println(type.getRegionName()); | |
System.out.println(type.getPoints()); | |
try{ | |
GeographicalInformation info = new GeographicalInformation(); | |
info.setResult("result"); | |
InputStream dbfStream = getClass().getResourceAsStream("/maps/"+type.getRegionName()+".dbf"); | |
InputStream shapeStream = getClass().getResourceAsStream("/maps/"+type.getRegionName()+".shp"); | |
DataSource shp = new ByteArrayDataSource(IOUtils.toByteArray(shapeStream) ); | |
DataSource dbf = new ByteArrayDataSource(IOUtils.toByteArray(dbfStream) ); | |
info.setShapeData(new DataHandler(shp)); | |
info.setDbfData(new DataHandler(dbf)); | |
return info; | |
}catch(Exception e){ | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
-------- | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.cxf.interceptor.LoggingInInterceptor; | |
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; | |
public class ClientExample { | |
public static void main(String... args) throws Exception { | |
Map<String, Object> props = new HashMap<String, Object>(); | |
props.put("mtom-enabled", Boolean.TRUE); | |
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); | |
factory.setAddress("http://localhost/myService"); | |
factory.setProperties(props); | |
factory.setServiceClass(GeographicalService.class); | |
factory.getInInterceptors().add(new LoggingInInterceptor()); | |
GeographicalService service = (GeographicalService) factory.create(); | |
GeographicalRequest req = new GeographicalRequest(); | |
req.setRegionName("mexico"); | |
GeographicalInformation result = service.lookupGeography(req); | |
byte[] dbf = readAsBytea(result.getDbfData().getInputStream()); | |
byte[] shp = readAsBytea(result.getShapeData().getInputStream()); | |
} | |
private static byte[] readAsBytea(InputStream inputStream) | |
throws IOException { | |
byte[] shp = IOUtils.toByteArray(inputStream); | |
inputStream.close(); | |
return shp; | |
} | |
} | |
------------------------- | |
import javax.activation.DataHandler; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlMimeType; | |
import javax.xml.bind.annotation.XmlType; | |
@XmlType | |
public class GeographicalInformation { | |
private String result; | |
private DataHandler shapeData; | |
private DataHandler dbfData; | |
public String getResult() { | |
return result; | |
} | |
public void setResult(String result) { | |
this.result = result; | |
} | |
@XmlMimeType("application/shp") | |
@XmlElement(name="shp") | |
public DataHandler getShapeData() { | |
return shapeData; | |
} | |
public void setShapeData(DataHandler shapeData) { | |
this.shapeData = shapeData; | |
} | |
@XmlMimeType("application/dbf") | |
@XmlElement(name="dbf") | |
public DataHandler getDbfData() { | |
return dbfData; | |
} | |
public void setDbfData(DataHandler dbfData) { | |
this.dbfData = dbfData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment