How can I get a file from the server in Java service

Hi all, I want to know how can I get a file from server in java service.

I am doing this code but I am not able to get the file

try

{

context=new Context();
context.connect(server,username,password);
} catch(Exception e) {
e.printStackTrace();
thrownew ServiceException("\n"+“Could not connect to the Server”);
}

try {
//file=new File("");
[SIZE=2]file=

[/size]

new File(“filePath”);

//file=new File(“C:/eclipse/work/eclipse/workspace351/Cache/config/ehcache.xml”);
//file=new File("");
[SIZE=2]}

[/size]

catch(Exception e) {

e.printStackTrace();
thrownew ServiceException("\n"+“Could not find file”);

}

When I am trying to check if the file exists on not. I am getting “False”

[SIZE=2]System.out.println("File exist - "+file.exists());

Please give me the code, how to do it.

Thanks,
Venkat.
[/SIZE]

Hi Venkat,

More than answers, I have a few questions to you before we could give you a solution.

  1. Where is the file present?
  2. Where are you running this code from, IS or external?
  3. Is this the whole code or snippets?

If you are trying to load/read a file that is present in the filesystem on IS then you can use the built-in services available. Refer built-in services guide. You dont need to use context and connect to the IS if you are already running this code from IS itself.

If you are trying to access this file on IS from external systems, then you have a few ways of doing, using Java, webservices, etc.,. Refer Java API guide and webservices guide for this.

HTH
-Suren

As Suren mentioned above, you will have to define your requirement more clearly. What is the need to create a custom service to get file when you have a publicly available service? pub.file:getFile?

I am assuming that you need a service to get file from a folder and then provide a Java client code to an external application for calling this service (since you are talking about context and stuff).

Use the readFileToString.txt for creating a java service in webMethods. After creating that service, create a Java client code by right clicking on the Java editor (using Designer or click tools → generate code-> in Developer) and select “For calling this service from a client” option and click ok. This will generate the client code to call the readFileToString WM Java service.

I would strongly suggest you go through the webMethods API documentation as the Context is used only when there is a need to build CLIENTS that invoke services on IS.

Input: fileName
output: outputString

Cheers,
Akshith

Hi, I am using getFile java service for getting the file data that is there on the Unix under the package in which I am developing my java service.

The code is written on IS itself and the file exist on the Unix system and in the same package where I am writing code, i.e, DeveloperPOC. See below

\apps\wm\ENT_IS2\webMethods80\IntegrationServer\packages\DeveloperPOC\config\ehcache.xml

But it is saying the file does not exists, I dont know what is wrong with it. Can you tell me do I need to get the file using SSH commands and then append the data to a local file.

If so then there is no point in using “getFile” java service.

Also if I dont use “Context” then how can I invoke the “getFile” built-in java service. Here is the code.

IDataUtil.put(inputCursor,

“filename”, xmlFileName);

IDataUtil.put(inputCursor,“loadAs”, “string” );

try {
result=context.invoke(“pub.file”,“getFile”,input);
} catch(Exception e) {
e.printStackTrace();
thrownew ServiceException("\n"+“Could not invoke the file”);
}

It is giving me Service Exception as

[SIZE=2]\apps\wm\ENT_IS2\webMethods80\IntegrationServer\packages\DeveloperPOC\config\ehcache.xml
File exist - false
[U][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]com.wm.app.b2b.server.ServiceException

[/color][/size][/COLOR][/SIZE][/u]

: [ISS.0086.9256] File [\apps\wm\ENT_IS2\webMethods80\IntegrationServer\packages\DeveloperPOC\config\ehcache.xml] does not exist[SIZE=2][COLOR=#ff0000]

at pub.CommonUtils.checkFileExists(

[/COLOR][/SIZE]

CommonUtils.java:448)[SIZE=2][COLOR=#ff0000]

at pub.file.getFile(

[/COLOR][/SIZE]

file.java:972)[SIZE=2][COLOR=#ff0000]

at sun.reflect.GeneratedMethodAccessor90.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(

[/COLOR][/SIZE]

DelegatingMethodAccessorImpl.java:25)[SIZE=2][COLOR=#ff0000]

at java.lang.reflect.Method.invoke(

[/COLOR][/SIZE]

Method.java:597)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.JavaService.baseInvoke(

[/COLOR][/SIZE]

JavaService.java:439)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.InvokeManager.process(

[/COLOR][/SIZE]

InvokeManager.java:635)[SIZE=2][COLOR=#ff0000]

at codecoverage.base.CodeCoverageProcessor.process(

[/COLOR][/SIZE]

CodeCoverageProcessor.java:191)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.util.tspace.ReservationProcessor.process(

[/COLOR][/SIZE]

ReservationProcessor.java:46)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.StatisticsProcessor.process(

[/COLOR][/SIZE]

StatisticsProcessor.java:44)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.ServiceCompletionImpl.process(

[/COLOR][/SIZE]

ServiceCompletionImpl.java:238)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.ValidateProcessor.process(

[/COLOR][/SIZE]

ValidateProcessor.java:51)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.PipelineProcessor.process(

[/COLOR][/SIZE]

PipelineProcessor.java:171)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.ACLManager.process(

[/COLOR][/SIZE]

ACLManager.java:272)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.DispatchProcessor.process(

[/COLOR][/SIZE]

DispatchProcessor.java:30)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.AuditLogManager.process(

[/COLOR][/SIZE]

AuditLogManager.java:399)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.InvokeManager.invoke(

[/COLOR][/SIZE]

InvokeManager.java:539)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.invoke.InvokeManager.invoke(

[/COLOR][/SIZE]

InvokeManager.java:384)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.ServiceManager.invoke(

[/COLOR][/SIZE]

ServiceManager.java:234)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.ServiceManager.invoke(

[/COLOR][/SIZE]

ServiceManager.java:109)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.HTTPInvokeHandler._process(

[/COLOR][/SIZE]

HTTPInvokeHandler.java:150)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.InvokeHandler.process(

[/COLOR][/SIZE]

InvokeHandler.java:110)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.HTTPDispatch.handleRequest(

[/COLOR][/SIZE]

HTTPDispatch.java:190)[SIZE=2][COLOR=#ff0000]

at com.wm.app.b2b.server.Dispatch.run(

[/COLOR][/SIZE]

Dispatch.java:350)[SIZE=2][COLOR=#ff0000]

at com.wm.util.pool.PooledThread.run(

[/COLOR][/SIZE]

PooledThread.java:131)[SIZE=2][COLOR=#ff0000]

at java.lang.Thread.run(

[/COLOR][/SIZE]

Thread.java:619)[SIZE=2][COLOR=#ff0000]

[/color][/size][U][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]com.wm.app.b2b.server.ServiceException

[/color][/size][/COLOR][/SIZE][/u]

: [SIZE=2][COLOR=#ff0000]

Could not invoke the file
at com.dish.cache.Cache.main(

[/COLOR][/SIZE]

Cache.java:114)[SIZE=2][COLOR=#ff0000]

[/color][/size][U][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]com.wm.app.b2b.server.ServiceException

[/color][/size][/COLOR][/SIZE][/u]

: [SIZE=2][COLOR=#ff0000]

File data does not exist
at com.dish.cache.Cache.main(

[/COLOR][/SIZE]

Cache.java:170)[SIZE=2][COLOR=#ff0000]

[/color][/size][U][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]com.wm.app.b2b.server.ServiceException

[/color][/size][/COLOR][/SIZE][/u]

: [SIZE=2][COLOR=#ff0000]

Could not append data to pipeline
at com.dish.cache.Cache.main(

[/COLOR][/SIZE]

Cache.java:182)[SIZE=2][COLOR=#ff0000]

Exception in thread “main”

[/COLOR][/SIZE]

java.lang.NullPointerException[SIZE=2][COLOR=#000080]

[/color][/size][SIZE=2][COLOR=#ff0000][SIZE=2][COLOR=#ff0000]at com.dish.cache.Cache.main(

[/color][/size][/COLOR][/SIZE]

Cache.java:188)

Can you suggest me how can I do this.

The task is that I need to get the file data of a file from the Unix.

Thanks,
Venkat.[SIZE=2][COLOR=#ff0000][COLOR=#ff0000]

[/color][/COLOR][/size][/SIZE]

Invoke the getfile service directly and provide the same inputs that you are using for the above java service and see if it can retrieve the data.

filename:\apps\wm\ENT_IS2\webMethods80\IntegrationServer\packages\DeveloperPOC\config\ehcache.xml
loadAs: String

Also, In one of the above posts Suren answered your other question about context. Refer to the Java API doc for more info about Context calss.

Cheers,
Akshith

Hi, I am sorry I am looking at the APIs but I am not able to figure out how it should be done. Can you please help me out

I am using Webmethods 8.0.

I just need to know how can I get the file from the Unix by giving the unix file path. Please see my code attached.

Please give me the code. I know I am asking too much from you guys. I am new to Webmethods technology. Please help me.

Please do not mind, I need to do it by afternoon.

Thanks,
Venkat.
Webmethod Cache Code.txt (4.26 KB)

Use forward slashes in the filename, not backslashes.

You use Context and TContext class methods to build clients that invoke services on IS. You should use Service class methods to access Objects that the IS services uses. Go through the Java API guide.

Here is a simple code to invoke the getFile service and print the file content on fileString variable.

 
 
String string = "";
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
 String filename = IDataUtil.getString( pipelineCursor, "filename" );
 String  loadAs = IDataUtil.getString( pipelineCursor, "loadAs" );
pipelineCursor.destroy();
// input
IData input = IDataFactory.create();
IDataCursor inputCursor = input.getCursor();
IDataUtil.put( inputCursor, "filename", filename );
IDataUtil.put( inputCursor, "loadAs", loadAs );
inputCursor.destroy();
// output
IData  output = IDataFactory.create();
try{
 output = Service.doInvoke( "pub.file", "getFile", input );
}catch( Exception e){
string = e.toString();
}
IDataCursor outputCursor = output.getCursor();
 // body
 IData body = IDataUtil.getIData( outputCursor, "body" );
 if ( body != null)
 {
  IDataCursor bodyCursor = body.getCursor();
   string = IDataUtil.getString( bodyCursor, "string" );
  bodyCursor.destroy();
 
 }
outputCursor.destroy();
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "fileString", string);
pipelineCursor_1.destroy();
 

First make sure this service reads the file properly and then move on to the ehcache.Also keep in mind about the slashes that Rob points out.

BTW, if you could please let me know which document that speaks about the this ehcache stuff.

HTH
-Suren