Re: Sample Java code for Trim web services (1045 Views)
Reply
Occasional Contributor
sai_kris
Posts: 6
Registered: ‎03-12-2009
Message 1 of 6 (1,581 Views)
Accepted Solution

Sample Java code for Trim web services

Hi All,

I am trying to upload a file in to TRIM using TRIM web services in Java. I am strcking in many places.

Kindly share with me if any one has Sample Java code to upload files using web services.

Thaks in advance

Best Regards
Sai
Please use plain text.
Esteemed Contributor
Richard Kelly_7
Posts: 203
Registered: ‎02-15-2007
Message 2 of 6 (1,581 Views)

Re: Sample Java code for Trim web services

Sai,
Don't know what you are using as a development environment, but I would strongly recommend NetBeans - it's support for web services is awesome and no need to run any of the external wsimport type commands. The attached code, extracted from a larger project does a basic record creatin, file upload and checkin, which I guess is what you need. Some of the names were generated by NetBEans so may differ from your own.

Richard
Note: Any posts I make on this forum are my own personal opinion and do not constitute a formal commitment on behalf of HP.
Please use plain text.
Occasional Contributor
sai_kris
Posts: 6
Registered: ‎03-12-2009
Message 3 of 6 (1,581 Views)

Re: Sample Java code for Trim web services

Hi Richard,

Thank you a lot for your prompt response and code is very useful to me.I have a question on sending credentials in Java.

we use System.Net.Credential("username","password") in .net to pass credentials , what is the equivalent command in Java to pass.

Thanks in advance

Best Regards
Sai
Please use plain text.
Esteemed Contributor
Richard Kelly_7
Posts: 203
Registered: ‎02-15-2007
Message 4 of 6 (1,581 Views)

Re: Sample Java code for Trim web services

Sai,
Its a few months since I wrote it, but I think you will find that the default authentication is NTLM. You can set a handler to catch failures and then specify a userid and password.

Setting the handler:

Authenticator.setDefault(new MyAuthenticator());

... somewhere before you call and web service functions.

Sample authenticator process:

static class MyAuthenticator extends Authenticator
{
@Override
public PasswordAuthentication getPasswordAuthentication ()
{
SecurePrompt sp = new SecurePrompt (null, "Site "+ getRequestingSite() +
" has requested " + getRequestingScheme() + " Authentication");
sp.setVisible(true);
if (sp._uid.getText().isEmpty())
return (null);



return (new PasswordAuthentication (sp._uid.getText(), sp._pwd.getPassword()));
}

}

SecurePrompt is just a function to retrieve a userid and password......
Note: Any posts I make on this forum are my own personal opinion and do not constitute a formal commitment on behalf of HP.
Please use plain text.
Occasional Visitor
rajasekhar908
Posts: 1
Registered: ‎04-01-2013
Message 5 of 6 (1,320 Views)

Re: Sample Java code for Trim web services

[ Edited ]

Authenticator auth = new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
System.out.println("in getting password");
//return super.getPasswordAuthentication();
String username = "hptrim";
String password = "password";
return new PasswordAuthentication(username, password.toCharArray());
}

@Override
protected URL getRequestingURL() {
// TODO Auto-generated method stub
System.out.println("in url");
return super.getRequestingURL();
}

@Override
protected RequestorType getRequestorType() {
// TODO Auto-generated method stub
return super.getRequestorType();
}

};
Authenticator.setDefault(auth);

String url = "http://localhost:82/trimws/trim.asmx?wsdl";

java.net.URL host = null;

try {
host = new java.net.URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



BufferedReader in = new BufferedReader(new InputStreamReader(host.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();

 

Above working fine, it is reading the WSDL

 

Code :Using SOAP Engine Stub.

 

Authenticator auth = new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
System.out.println("in getting password");
//return super.getPasswordAuthentication();
String username = "hptrim";
String password = "password";
return new PasswordAuthentication(username, password.toCharArray());
}

@Override
protected URL getRequestingURL() {
// TODO Auto-generated method stub
System.out.println("in url");
return super.getRequestingURL();
}

@Override
protected RequestorType getRequestorType() {
// TODO Auto-generated method stub
return super.getRequestorType();
}

};
Authenticator.setDefault(auth);

String url = "http://localhost:82/trimws/trim.asmx?wsdl";

java.net.URL host = null;

try {
host = new java.net.URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

 

Engine engine=new EngineLocator();

EngineSoap engineSoap=engine.getEngineSoap(host);

String st = engine.getEngineSoapAddress();
System.out.println("After:" +st);


TrimRequest request = new TrimRequest();

RecordStringSearchClause clause = new RecordStringSearchClause();
clause.setType(RecordStringSearchClauseType.TitleWord);
clause.setArg("reef");

// Construct a record search, and put our search clause in it
RecordSearch search = new RecordSearch();
search.setRecordStringSearchClause(clause);

//request.setRecordSearch(search);


TrimResponse response=new TrimResponse();

response=engineSoap.execute(request);

 

 

Error:

 

Exception in thread "main" AxisFault
faultCode: {http://xml.apache.org/axis/}HTTP
faultSubcode:
faultString: (401)Unauthorized
faultActor:
faultNode:
faultDetail:
{}:return code: 401
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
</fieldset></div>
</div>
</body>
</html>

{http://xml.apache.org/axis/}HttpErrorCode:401

(401)Unauthorized

 

Any help is appreciated.

 

--

Rajasekhar

 

Please use plain text.
Occasional Visitor
Chandranath
Posts: 1
Registered: ‎06-23-2013
Message 6 of 6 (1,045 Views)

Re: Sample Java code for Trim web services

Hi Experts,

 

I'm also facing the same issue with the code.

 

One of our customer doesn't want to use .NET application and so we need to write code in Java to call TRIM Web Service. Can anybody please tell me how to pass credentials? In .NET, we use:

 

engine.Credentials = new System.Net.NetworkCredential(username, password);

 

What will be the Java equivalent?

 

Thanks,

Chandranath

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation