cancel
Showing results for 
Search instead for 
Did you mean: 

Get a PDF with FileDownload UI on demand (Bug???)

Former Member
0 Kudos

Hi,

my purpose is to download a pdf from my back-end system (content server) on demand. I use a FileDownload UI with the two Methods below.

The first Method "getOnDemandStream" triggerd only when i click on the FileDownload link.

My problem is that i get not the right inputStream back, so the result looks like this:

--ejjeeffe0

content-Type: application/pdf

content-length: 993051

x-comId: KM_Programming.pdf

%pdf1.2

%ÄEIO

stream H%}..........

Can somebody give advice what i could look at ?



public com.sap.tc.webdynpro.progmodel.api.IWDInputStream
  getOnDemandStream(IPrivateDPView.IContextElement element)
{
	ByteArrayOutputStream outputStream = null;
	try {			
		URL url = new java.net.URL( e.getUrl() );
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		outputStream = new ByteArrayOutputStream();
		int length;			
		byte[] part = new byte[1024 * 4];
		while ((length = is.read(part)) != -1) {
			outputStream.write(part, 0, length);
		}						
		is.close();		
	} catch (Exception ex)	{		
	}		
	return WDResourceFactory.createInputStream( outputStream.toByteArray() );
}

 
public void wdDoInit()
{
		IWDAttributePointer attributePointer = 
			wdContext.currentContextElement().getAttributePointer("onDemandStream");
	
		IWDResource resource = WDResourceFactory.createResource(
				attributePointer,
				"test.pdf",
				WDWebResourceType.PDF
				);	
 
		wdContext.currentContextElement().setResource(resource);
}

regards,

Sharam

Accepted Solutions (0)

Answers (1)

Answers (1)

SRudra
Contributor
0 Kudos

Hi,

The code looke fine. A few pointers to the problem:

1. Check if the pdf file is generated properly on the server / location from where you are trying to download.

2. Check if there a security restiction, to download files in your LAN setting from a specific server / location. Confirm this from the LAN administrator.

BTW, let us know if you are using ADS or some third party frameworks like POI / FOP etc to generate the pdf file.

regds,

Sukanta Rudra

Former Member
0 Kudos

Hallo Sukanta,

thanks for your relplay!

> 1. Check if the pdf file is generated properly on the

> server / location from where you are trying to

> download.

If i put the url (e.getURL()) into my browser i can download the file.

> 2. Check if there a security restiction, to download

> files in your LAN setting from a specific server /

> location. Confirm this from the LAN administrator.

I think there are no special security setting, because of the first step.

> BTW, let us know if you are using ADS or some third

> party frameworks like POI / FOP etc to generate the

> pdf file.

I get the pdf file from sdn. I was just generated by sap.

The method "getOnDemandStream" is from type "IWDInputStream" Could it be the problem? I try also to cast my InputSteram to IWDInputStream and change also read(buf) to read(buf,0,length) with the result: NullPointerException.


  public com.sap.tc.webdynpro.progmodel.api.IWDInputStream getOnDemandStream(IPrivateDPView.IContextElement element)
  {
	ByteArrayOutputStream outputStream = null;
	try {			
		URL url = new java.net.URL( e.getUrl() );
		URLConnection conn = url.openConnection();  
                IWDInputStream is = (IWDInputStream) conn.getInputStream(); 
		outputStream = new ByteArrayOutputStream();
		int length = 0;			
		byte[] buf = new byte[1024 * 10];
		while ((length = is.read(buf,0,length)) != -1) {
			outputStream.write(buf, 0, length);
		}			
		is.close();		
		
	} catch (Exception ex)	{		
	}		
	return WDResourceFactory.createInputStream( outputStream.toByteArray() );
}

regards,

Sharam

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hallo Sharam,

In my newest tutorial on <i>Downloading Files in Tables on demand</i> I invoced the same methods; the only difference was, that I called

public static IWDInputStream createInputStream(java.io.InputStream data)

instead of

public static IWDInputStream createInputStream(byte[] data)

. The calculated context attribute getter method must return an object of type IWDInputStream.

What's

IWDInputStream is = (IWDInputStream) conn.getInputStream();

? You should not cast to IWDInputStream here, use java.io.InputStream instead. Why do you create an object of type IWDInputStream twice?

Regards, Bertram

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

A similar reply was posted by Valery Silaev in a sibling thread on the same issue, which is meanwhile blocked:

Sharam,

I dont' understand your coding completely, however one line in getter catches my attention:


stream = (IWDInputStream) is;  <--- ???

With this line you should always get ClassCastException (instead of NullPointerException). So I'm wondering how your code works with local file -- seems that calculated attribute getter used absolutely different coding previously.

Anyway, replace this line with:


stream = WDResourceFactory.createInputStream( outputStream.toByteArray() );

Or better yet, replace complete method:


public com.sap.tc.webdynpro.progmodel.api.IWDInputStream
  getOnDemandStream(IPrivateDPView.IContextElement element)
{
  try {			
    final java.net.URL url = new java.net.URL( e.getUrl() );
    return WDResourceFactory.createInputStream( url.openStream() );
  } catch (java.io.IOException ex) {		
    wdComponentAPI.getMessageManager().reportException(
      new WDNonFatalException(ex), false
    );
    return null;
  }
}

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Reply from Sharam on Valery's message in the locked sibling thread on the same issue:

Hello Valery,

if I try it with the following piece of code I get always instead of my pdf document the following message:

--ejjeeffe0

content-Type: application/pdf

content-length: 993051

x-comId: KM_Programming.pdf

%pdf1.2

%ÄEIO

stream H%}..........

so I can not see the pdf document. What could be the problem?


public com.sap.tc.webdynpro.progmodel.api.IWDInputStream
  getOnDemandStream(IPrivateDPView.IContextElement element)
{
  try {			
    final java.net.URL url = new java.net.URL( e.getUrl() );
    return WDResourceFactory.createInputStream( url.openStream() );
  } catch (java.io.IOException ex) {		
    wdComponentAPI.getMessageManager().reportException(
      new WDNonFatalException(ex), false
    );
    return null;
  }
}

  public void wdDoInit()
  {
		IWDAttributePointer attributePointer = 
			wdContext.currentContextElement().getAttributePointer("onDemandStream");
	
		IWDResource resource = WDResourceFactory.createResource(
				attributePointer,
				"test.pdf",
				WDWebResourceType.PDF
				);	

		wdContext.currentContextElement().setResource(resource);
}

regards,

Sharam