cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

SOAP WS with Attachment

rzemmali
11-Garnet

SOAP WS with Attachment

Hello,

 

I created a SOAP WS with attachment parameter following this article:

http://support.ptc.com/help/windchill/whc/whc_en/index.html#page/Windchill_Help_Center%2FIEUG_WSFUseSOAPAttach.html%23

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.

1 ACCEPTED SOLUTION

Accepted Solutions
imendiola
12-Amethyst
(To:rzemmali)

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:

  • %WT_HOME%\prog_examples\jws\MyProject\src\build.xml --> ant script to compile and deploy the WS
  • %WT_HOME%\prog_examples\jws\MyProject\src\org\myorg\MyWService.java  --> main class of your WS
  • %WT_HOME%\prog_examples\jws\MyProject\src_client\build.xml  --> ant script to compile and generate a java client for tyour WS
  • %WT_HOME%\prog_examples\jws\MyProject\src_client\org\myorg\MyClient.java  --> main class for the client of the WS

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:

  • A new jar file in <WT_HOME>/codebase/WEB-INF/lib with the necessary compiled files of the WS
  • A block of code added to the file <WT_HOME>/codebase/WEB-INF/sun-jaxws.xml referencing the new WS available

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

 

 

http://www.prambanan-it.comIker Mendiola - Prambanan IT Services

View solution in original post

14 REPLIES 14
imendiola
12-Amethyst
(To:rzemmali)

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

http://www.prambanan-it.comIker 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
     

imendiola
12-Amethyst
(To:rzemmali)

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

http://www.prambanan-it.comIker 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

imendiola
12-Amethyst
(To:rzemmali)

I have the params defined on the WS like this one:

 

@WebParam(name = "dataHandler") DataHandler dataHandler

Using javax.activation.DataHandler class...

http://www.prambanan-it.comIker Mendiola - Prambanan IT Services

and in the windchill task how did you defined the web parameter?

imendiola
12-Amethyst
(To:rzemmali)

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();
        }
}
}

http://www.prambanan-it.comIker 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.

 

 

imendiola
12-Amethyst
(To:rzemmali)

Sorry.... I haven't created any Info*Engine based WS...

http://www.prambanan-it.comIker 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.

imendiola
12-Amethyst
(To:rzemmali)

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:

  • %WT_HOME%\prog_examples\jws\MyProject\src\build.xml --> ant script to compile and deploy the WS
  • %WT_HOME%\prog_examples\jws\MyProject\src\org\myorg\MyWService.java  --> main class of your WS
  • %WT_HOME%\prog_examples\jws\MyProject\src_client\build.xml  --> ant script to compile and generate a java client for tyour WS
  • %WT_HOME%\prog_examples\jws\MyProject\src_client\org\myorg\MyClient.java  --> main class for the client of the WS

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:

  • A new jar file in <WT_HOME>/codebase/WEB-INF/lib with the necessary compiled files of the WS
  • A block of code added to the file <WT_HOME>/codebase/WEB-INF/sun-jaxws.xml referencing the new WS available

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

 

 

http://www.prambanan-it.comIker 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?

imendiola
12-Amethyst
(To:rzemmali)

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

http://www.prambanan-it.comIker 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

Top Tags