cancel
Showing results for 
Search instead for 
Did you mean: 

Code Challange - Code for extracting content from a file in KM

Former Member
0 Kudos

Hi,

I want to extract content from a file which is in KM Repository using the Webdynpro View. I wrote the following code for the extraction. But it is throwing some exception.

Code Written:

<b>

URLConnection conn = null;

DataInputStream data = null;

String line;

StringBuffer buf = new StringBuffer();

wdContext.currentContextElement().setTextdisp("Hello");

try {

URL theURL = new URL("http://ctssap1:50000/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/News/New%20text.txt");

conn = theURL.openConnection();

// conn.connect();

wdContext.currentContextElement().setTextdisp("Inside Try");

data = new DataInputStream(new BufferedInputStream(

conn.getInputStream()));

while ((line = data.readLine()) != null) {

buf.append(line + "\n");

}

data.close();

String buffer = new String(buf);

wdContext.currentContextElement().setTextdisp(buffer);

}

catch (IOException e) {

wdContext.currentContextElement().setTextdisp("IO Error:" + e.getMessage());

}</b>

Exception Got:

<b>IO Error:Server returned HTTP response code: 401 for URL: http://ctssap1:50000/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/News/New%20text.txt...;

Can anyone help me out in this process... or please suggest some other way to extract the content.

Thanks in Advance.

Regards,

Srinivas.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Srinivasa .

1)This RID is the path in the KM repository where you are uploading your documents.This we will be specifying when we are creating an instance of ICollection.

The code is as shown below

IResourceFactory factory =

ResourceFactory.getInstance();

ICollection folder = (ICollection) factory.getResource(<b>rid</b>,context);

If you have the path as a vontext attribute you can assign that to your variable of type RID.

Kindly refer to my weblog

/people/rohit.radhakrishnan/blog/2005/05/27/uploading-files-to-km-repository-using-webdynpro-apis

Hope this clears your doubt

Regards

Rohit

Former Member
0 Kudos

Hi,

U can use this code to get the content of a file in KM

IWDClientUser wdClientUser = WDClientUser.getCurrentUser();

IUser sapUser = wdClientUser.getSAPUser();

IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);

IResourceContext resourceContext = new ResourceContext(ep5User);

IResourceFactory resourceFactory = ResourceFactory.getInstance();

RID pathRID = RID.getRID(wdContext.currentContextElement().getVaPath());

IResourceContext context = new ResourceContext(ep5User);

URI uri = null;

uri = uri.add("/documents");

ICollection parent = (ICollection) ResourceFactory.getInstance().getResource(uri, context);

IContent content = parent.getContent();

In the URI specify the path of ur file in the KM REpository.

Regards,

Sowjanya.

Former Member
0 Kudos

Hi Sowjanya,

Thanks for your reply.

I need to know about the following:

<b> 1. RID pathRID = RID.getRID(wdContext.currentContextElement().getVaPath());

For the above code, I have to first create a context element 'VaPath'. Can you please tell me exactly what it contains?

2. Where are we using the variable "pathRID".

3. How can i extract the content from variable "content" of type IContent into variable of type String.</b>

Please help me out in this.

Thanks once again.

Cheers,

Srinivas

Former Member
0 Kudos

Hi Srinivas,

VaPath will have the path of ur document.

Check this modified code snippet which will answer ur questions and solve ur problem.

IWDClientUser wdClientUser = WDClientUser.getCurrentUser();

IUser sapUser = wdClientUser. getSAPUser();

com.sapportals.portal.security.usermanagement.IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);

IResourceContext context = new ResourceContext(ep5User);

/Specify the path of ur document here./

RID pathRID = RID.getRID("/documents/file2");

IResource resource = ResourceFactory.getInstance()

.getResource(context);

InputStream in = resource.getContent().getInputStream();

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];

int bytesread = 0;

while ((bytesread = in.read(buffer)) != -1) {

out.write(buffer, 0, bytesread);

}

String myData = out.toString();

/*myFile will containS the content of the document./

wdComponentAPI.getMessageManager().reportSuccess(myData);

Hope this solves ur problem.

Regards,

Sowjanya.

Former Member
0 Kudos

Hello Sowjanya,

Thanks for your quick response.

I tried the above code, but it is giving me some problem.

IResource resource = ResourceFactory.getInstance()

.getResource(context);

Method getResource only with context parameter is not available, so i have used the menthod getResource(pathRID, context) as shown below.

<b>IResource resource = ResourceFactory.getInstance()

.getResource(pathRID, context);</b>

While executing this statement it is giving an error stating that <b>"Unable to generate View.".</b> Problem is only with the above line of code.

Please let me know if i did something wrong and help me in solving this problem.

Thanks once again.

Cheers... Srinivas

Former Member
0 Kudos

Hi,

I missed out the pathRid in teh method as aprameter.

What u wrote now is correct.

If the path which u r specifying is correct then

there will not be any problem in that line of code.

Did u give the sharing references in the webdynpro references of ur project .

PORTAL:sap.com/com.sap.km.application.

U will get a problem Failed to create delegate for view if u dont specify the SharingReference.

Regards,

Sowjanya.

Former Member
0 Kudos

Hi Sowjanya,

I tried the below line of code in my program and came to know that the method "getEP5User()" has got deprecated. Is there anyother alternative way by which I can convert the existing user to an EP5User. I need this because I have to work with KM specific user objects.

IUser ep5User =

WPUMFactory.getUserFactory().getEP5User(sapUser);

Right now I'm working on NWDS SP9. Is there any version compatibility issues with that??

Any input in this regard will be of great help to me.

Thanks in advance

Mausam

Former Member
0 Kudos

Hi,

Its not a problem I think that will work.It is still supported.Even if it is deprecated I am able to get the Iresource object by passing the ep5User.

Regards,

Sowjanya.

Former Member
0 Kudos

Hi,

Thanks for the immediate reply.

I'm getting "java.lang.NullPointerException" when I tried to get the instance of UserFactory itself.

IUserFactory uf = WPUMFactory.getUserFactory();

So, not able to get EP5User. And hence no ResourceContext.

I've imported "com.sap.security.api.ep5.jar" into NWDS SP9 and added it to the buid path of my project(as it was not there). Is this the cause for my problem? Do i need to add any other JAR files for accessing usermanagement APIs.

Regards,

Mausam

Former Member
0 Kudos

Hi,

These are the jars I added

prtapi.jar

com.sap.security.api.ep5.jar

com.sap.security.api.jar

bc.rf.framework_api.jar

bc.rf.global.service.urlgenerator_api.jar

bc.sf.framework_api.jar

bc.util.public_api.jar

com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();

//create an ep5 user from the retrieved user

com.sapportals.portal.security.usermanagement.IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);

Regards,

Sowjanya.

Former Member
0 Kudos

Hi,

I had added all the JAR files specified by you before itself except "prtapi.jar". Now I've included that also. But there is some problem with "getUserFactory()" method. It is returning NULL. And because of that I'm not able to get the EP5User.

Have you come across anything like this? And User Management APIs are deprecated (but that shouldnt cause any problem),so where they have been shifted to?

Any clue in this matter will be highly helpful.

Thanks and Regards,

Mausam

Former Member
0 Kudos

Hello Sowjanya,

Thanks sowjanya, now i am able to extract content.

I have some http links in my document. I want them to be displayed as weblinks after the extraction process.

Can you please tell me how can i do it?

Thanks in advance.

Cheers...

Srinivas

Former Member
0 Kudos

401 means that you unauthorized.

You have to supply credentials when accessing KM.

See SDN Weblogs https://www.sdn.sap.com/sdn/weblogs.sdn -- there are samples how work with KM from WebDynpro

VS