Support Article
PublicAPI sendFile Content-Type problem
SA-11444
Summary
Using the Pega PublicAPI sendFile(byte[] aFileData, java.lang.String aFileName, boolean aPersistFileToServiceExport, StringMap aHttpHeaders, boolean aSendForDownload) causes issues with the HTTP headers.
Setting Content-Type in the aHttpHeaders parameter always sends a response with Content-Type text/html;charset=UTF-8.
It appears that sendFile() does not handle the Content-Type setting correctly.
Error Messages
Not Applicable
Steps to Reproduce
Use the following code snippet:
byte[] byteArray=Base64Util.decodeToByteArray( base64String );
HashStringMap aMap = new HashStringMap();
aMap.put( "ContentDisposition", "inline;filename=\"aaa\"" );
aMap.put( "ContentType", "application/pdf" );
String sErrorMessage = tools.sendFile( byteArray, "aaa", false, aMap, false );
if( sErrorMessage != null )
{
tools.getThread().getThreadPage().putString( "pyXMLStream", sErrorMessage );
pega.terminateActivity();
}
HashStringMap aMap = new HashStringMap();
aMap.put( "ContentDisposition", "inline;filename=\"aaa\"" );
aMap.put( "ContentType", "application/pdf" );
String sErrorMessage = tools.sendFile( byteArray, "aaa", false, aMap, false );
if( sErrorMessage != null )
{
tools.getThread().getThreadPage().putString( "pyXMLStream", sErrorMessage );
pega.terminateActivity();
}
View the generated output:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-dynaTrace-JS-Agent: true
Set-Cookie: Pega-RULES=H6AEC020E4FF52725F043638DA0055A91; Version=1; Comment="PegaRULES session tracking"; Path=/prweb
Content-Disposition: inline;filename="aaa"
ContentType: application/pdf
Cache-Control: max-age=0
Content-Encoding: gzip
Content-Type: text/html;charset=UTF-8
Content-Length: 23
Date: Fri, 19 Jun 2015 06:15:49 GMT
Server: Apache-Coyote/1.1
X-dynaTrace-JS-Agent: true
Set-Cookie: Pega-RULES=H6AEC020E4FF52725F043638DA0055A91; Version=1; Comment="PegaRULES session tracking"; Path=/prweb
Content-Disposition: inline;filename="aaa"
ContentType: application/pdf
Cache-Control: max-age=0
Content-Encoding: gzip
Content-Type: text/html;charset=UTF-8
Content-Length: 23
Date: Fri, 19 Jun 2015 06:15:49 GMT
Obviously, there is a header ContentType but its value should be with the header Content-Type. For instance, it works as expected for the Content-Disposition header.
Root Cause
The root cause of this problem is in a third-party product. But first, the code does not recognize ContentType as a key in the aHttpHeaders map parameter for sendFile(); it has to be contentType (lowercase 'c').
Nevertheless, the Content-Type header will be set by the web container (in this case, Tomcat 7) based on the file name extension, regardless of what is set by PRPC. If the file has no extension, as in the code sample shown above, the default is text/html:charset=UTF-8.
Resolution
As long as the file's extension is configured in Tomcat's web.xml, this should be sufficient to provide complete filenames (with an extension).
Published July 23, 2015 - Updated October 8, 2020
Have a question? Get answers now.
Visit the Collaboration Center to ask questions, engage in discussions, share ideas, and help others.