Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Hello,
I created a SOAP WS with attachment parameter following this article:
In the client side I passed a DataHandler object. But, when I read the DataSource in the WS task , it's always null.
Who have idea how to fix such issue? Is there a full sample for created SOAP WS with attachment?
Thank you.
Solved! Go to Solution.
Hi rzemmali,
if you follow PTC's guideline to create a Java based webservice, they say that you need to launch a command to create the WS related files. For example:
<WT_HOME> > ant -Dproject.dir=E:\ptc\Windchill_11.1\Windchill\prog_examples\jws\MyProject -Dservlet.name=MyWService -Dsecurity.policy=webServerAuthenticated -Dmain.class=org.myorg.MyClient -Dservice.type=java -Dservice.class=org.myorg.MyWService -f bin\adminTools\WebServices\new-project.xml create
Note: this command will create a webserver authenticated WS. So, you will need to pass user/password from the client to invoke it. You can create a WS that will be authenticated with keystores if you want to avoid user/pass
Basically, this command will create different files inside the folder E:\ptc\Windchill_11\Windchill\prog_examples\jws\MyProject:
And you should write the code in the java file generated. Anyway, if you already have you WS main java class and you want to use it, you just need to create the ant script to compile and deploy it. So, for example, if you have created in you typical src folder a class org.myorg.MyWSservice.java file, you need to create the ant script inside your src folder. You can call it, for example, MyWSservice.xml and the contents should be similar to:
<?xml version="1.0"?>
<!DOCTYPE project [
<!ENTITY ws_env SYSTEM "..\Windchill\bin\adminTools\WebServices\common\common-env.jws_xml">
<!ENTITY ws_server SYSTEM "..\Windchill\bin\adminTools\WebServices\common\common-server.jws_xml">
]>
<project name="MyWService" default="all">
<property name="webservice.class" value="org.myorg.MyWService" />
<property name="security.policy" value="webServerAuthenticated" />
&ws_env;
&ws_server;
</project>
NOTE: if you copy the content, please take care with the paths to the xml files referenced at the beginning of it. It should be the relative path from the location where the xml file is stored.
Once you have the ant script you can call it with the command:
<WT_HOME> > ant -f src/MyWService.xml
This command should compile and deploy your webservice, generating:
Restarting Windchill will do your WS available and you can check it accessing to the url:
http://<HOST_NAME>/Windchill/servlet/MyWService?wsdl
If you have problems editing manually the xml file or compilation errors, use the first method to let Windchill create all the necessary files and then add your code to the generated class.
Regards
Iker Mendiola - Prambanan IT Services |
Hi,
how are you passing the file from the WS client?
I wrote a WS that was receiving a DataHandler and passing the file from a client this way:
File file = new File("c:/temp/test.pdf");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream inputStream = new BufferedInputStream(fis);
byte[] fileBytes = new byte[(int) file.length()];
MyWSService service = new MyWSService();
MyWS port = service.getMyWSPort ();
returnValue = port.myMethod (fileBytes);
Hope this helps.
Regards
Iker Mendiola - Prambanan IT Services |
Hi,
The method generated from the wsdl receive a DataHandler object:
myMethod ( @WebParam(name = "file", targetNamespace = "")
DataHandler file)
;
So in client side, I'm doing this:
DataSource fds = new FileDataSource("C:\\work\\docs\\ws\\file.csv");
DataHandler handler = new DataHandler(fds);
MyWSService service = new MyWSService();
MyWS port = service.getMyWSPort ();
returnValue = port.myMethod (handler);
Regards,
Rawia
Hi,
Even declaring the param in the WS as DataHandler, try using this method in the client to send the file:
File file = new File("c:/temp/test.pdf");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream inputStream = new BufferedInputStream(fis);
byte[] fileBytes = new byte[(int) file.length()];
inputStream.read(fileBytes);
MyWSService service = new MyWSService();
MyWS port = service.getMyWSPort ();
returnValue = port.myMethod (fileBytes);
inputStream.close();
and then use this method in the server to retrieve the file from the received DataHandler (you can add the filename as parameter to the WS later to use it to save the file on the server):
String tempFolderPath = (String) WTProperties.getServerProperties().getProperty("wt.temp");
File tempFile = new File(tempFolderPath + File.separator + "MyFilename.txt");
try (InputStream input = dataHandler.getInputStream(); OutputStream output = new FileOutputStream(tempFile);) {
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = input.read(b)) != -1) {
output.write(b, 0, bytesRead);
}
// The file is stored in tmpFile
/* Logic */
} catch (Exception e) {
e.printStackTrace();
}
Regards
Iker Mendiola - Prambanan IT Services |
Hi,
Unfortunately it did not work in my case: setting byte[] param instead of DataHandler in the web method leads to compilation error.
In the server side the parameter is DataSource
@param javax.activation.DataSource file {contentType:text/csv}
I changed it to
javax.activation.DataHandler
and also changed the signature of the generated web method to receive a byte[] but I got this error:
Can not set javax.activation.DataHandler field to [B.
Regards,
Rawia
I have the params defined on the WS like this one:
@WebParam(name = "dataHandler") DataHandler dataHandler
Using javax.activation.DataHandler class...
Iker Mendiola - Prambanan IT Services |
and in the windchill task how did you defined the web parameter?
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.ptc.jws.servlet.JaxWsWebService;
@WebService()
public class MyWS extends JaxWsWebService
{
@WebMethod(operationName="myMethod")
public void myMethod(@WebParam(name = "dataHandler") DataHandler dataHandler) {
String tempFolderPath = (String) WTProperties.getServerProperties().getProperty("wt.temp");
File tempFile = new File(tempFolderPath + File.separator + "MyFilename.txt");
try (InputStream input = dataHandler.getInputStream(); OutputStream output = new FileOutputStream(tempFile);) {
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = input.read(b)) != -1) {
output.write(b, 0, bytesRead);
}
// The file is stored in tmpFile
/* Logic */
} catch (Exception e) {
e.printStackTrace();
}
}
}
Iker Mendiola - Prambanan IT Services |
Thanks for this snippet.
In my case did not a create Java-based Web Service, unstead an Info*Engine-based Web Service. which I convert it to a JAX-WS.
As in the section "BLOB Attachments on SOAP Requests" (from the first url), I added the file param
@param javax.activation.DataSource file {contentType:content_type}
and then I just tryed to read it , but the value was null.
Sorry.... I haven't created any Info*Engine based WS...
Iker Mendiola - Prambanan IT Services |
Hi,
I decided to switch to a java based ws. I implemented the ws classe into codebase package. Then I tryed to deploy it using ant command (ant -Dservlet.name=MyWebService -Dtype.id=com.ptc.windchill.ws ) but since the class is not in <Windchill>/bin/adminTools/WebServices the deployment failed (obvious!).
Is there a way to deploy this custom ws with windchill utilities?
Thanks for your support.
Hi rzemmali,
if you follow PTC's guideline to create a Java based webservice, they say that you need to launch a command to create the WS related files. For example:
<WT_HOME> > ant -Dproject.dir=E:\ptc\Windchill_11.1\Windchill\prog_examples\jws\MyProject -Dservlet.name=MyWService -Dsecurity.policy=webServerAuthenticated -Dmain.class=org.myorg.MyClient -Dservice.type=java -Dservice.class=org.myorg.MyWService -f bin\adminTools\WebServices\new-project.xml create
Note: this command will create a webserver authenticated WS. So, you will need to pass user/password from the client to invoke it. You can create a WS that will be authenticated with keystores if you want to avoid user/pass
Basically, this command will create different files inside the folder E:\ptc\Windchill_11\Windchill\prog_examples\jws\MyProject:
And you should write the code in the java file generated. Anyway, if you already have you WS main java class and you want to use it, you just need to create the ant script to compile and deploy it. So, for example, if you have created in you typical src folder a class org.myorg.MyWSservice.java file, you need to create the ant script inside your src folder. You can call it, for example, MyWSservice.xml and the contents should be similar to:
<?xml version="1.0"?>
<!DOCTYPE project [
<!ENTITY ws_env SYSTEM "..\Windchill\bin\adminTools\WebServices\common\common-env.jws_xml">
<!ENTITY ws_server SYSTEM "..\Windchill\bin\adminTools\WebServices\common\common-server.jws_xml">
]>
<project name="MyWService" default="all">
<property name="webservice.class" value="org.myorg.MyWService" />
<property name="security.policy" value="webServerAuthenticated" />
&ws_env;
&ws_server;
</project>
NOTE: if you copy the content, please take care with the paths to the xml files referenced at the beginning of it. It should be the relative path from the location where the xml file is stored.
Once you have the ant script you can call it with the command:
<WT_HOME> > ant -f src/MyWService.xml
This command should compile and deploy your webservice, generating:
Restarting Windchill will do your WS available and you can check it accessing to the url:
http://<HOST_NAME>/Windchill/servlet/MyWService?wsdl
If you have problems editing manually the xml file or compilation errors, use the first method to let Windchill create all the necessary files and then add your code to the generated class.
Regards
Iker Mendiola - Prambanan IT Services |
Hi Imendiola,
Thanks for your support. The second approch did not work , so I back to the first way and the ws is successfully generated. Then I created a client locally in my pc but I get security error : An Error occurred while locating PEER Entity certificate in TrustStore. Do you have idea how to fix it?
thank you?
I suppose that you are using certificate based authentication method from your client... I only have developed with user/password authentication, so I cannot help you with that error... sorry
Iker Mendiola - Prambanan IT Services |
Hi Iker,
It works. Thanks a lot.
However It's a new project in /prog_examples/jws/ dir so independant from my working directory. My purpose is to have the ws class under /opt/wt10/windchill/codebase.
Regards,
Rawia