Showing posts with label java sinppet. Show all posts
Showing posts with label java sinppet. Show all posts

Monday, October 5, 2009

Java Code Sinppet: JAX-WS & MTOM

To enable MTOM (Message Transmission Optimization Mechanism) there are several things need to be done and in a nutshell

1. add the MTOM annotation on the services class

@javax.xml.ws.soap.MTOM
@WebService(serviceName =


2. Following table list the relationship between MIME type and java type

MIME type Java type
image/gif java.awt.Image
image/jpeg java.awt.Image
text/plain java.lang.String
text/xml javax.xml.transform.Source
application/xml javax.xml.transform.Source
*/* javax.activation.DataHandler


3. Edit the WSDL to and list the correct mime type which will change the schema from

<xs:sequence>
<xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>

to

<xs:sequence>
<xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"
xmime:expectedContentTypes="image/jpeg" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"/>
</xs:sequence>


4. Make the container use the edited WSDL than generated one

@WebService(serviceName = ...., wsdlLocation="WEB-INF/wsdl/edited.wsdl")


5. Deploy the new service

for more info netbeans tutorial pages 1, page 2, page 3



To do the above without any modification to wsdl (using annotation on the java class)

1. Set the @MTOM as above on the service
@MTOM
@WebService
public class NewWebService {

2. Annotate the data type on the web method as follows
@WebMethod
public void setImage(@XmlMimeType("image/gif")Image img){

3. Can also annotate a return type
@WebMethod
public @XmlMimeType("image/gif")Image getImage( String name){

4. All kind of data types could be used
@WebMethod
public @XmlMimeType("*/*")Image getFiles


Wednesday, July 22, 2009

Java Code Sinppet: OWL Demo with SWRL

In the classpath :
protege.jar
and every jar in the
Protege_3.4\plugins\edu.stanford.smi.protegex.owl
directory
public class OWLDemo {

public static void main(String[] args) {
try {
String uri = "file:///C:/MSC/project/old/travel.owl";
// String uri = "http://localhost:8080/owl/travel.owl";
OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);
OWLNamedClass classs= owlModel.getOWLNamedClass("Trade_Association");

Collection c = classs.getNamedSubclasses();
Iterator itr = c.iterator();

while(itr.hasNext()){
OWLNamedClass cc = (OWLNamedClass) itr.next();
System.out.println(cc.getNestedBrowserText());
System.out.println(cc.getName());
}

SWRLFactory factory = new SWRLFactory(owlModel);
// factory.replaceImps(owlModel); // to run the same query again uncomment
SWRLImp imp = factory.createImp("Query-2", "Use_Inconjunction_With(?x, ?y) -> sqwrl:select(?x, ?y)");

SWRLRuleEngineBridge bridge = BridgeFactory.createBridge("SWRLJessBridge", owlModel);
bridge.infer();
SQWRLResult result = bridge.getSQWRLResult("Query-2");
while (result.hasNext()) {
System.out.println("X: " + result.getObjectValue("?y").getIndividualName());
System.out.println("Y: " + result.getObjectValue("?x").getIndividualName());

result.next();
}

} catch (Exception ex) {

}

}

}


A Useful Guide