Wednesday, March 11, 2015

iText Java - Document Metadata values are blank

Problem Description:

Using iText java to create PDF with meta-data values as follows. PDF created successfully however the meta-data values are empty or blank.

Document document = new Document(PageSize.A4, -55, -65, 20, 7);
document.addAuthor("pdfAuthor");
document.addCreator("pdfCreator");
document.addCreationDate();
document.addTitle("Title");
document.addSubject("Subject");
document.addKeywords("Keywords");

 
PdfWriter writer = null;
ByteArrayOutputStream byteOut = null;

byteOut = new ByteArrayOutputStream();                           
writer = PdfWriter.getInstance(document, byteOut);

document.open();

Solution:

Always add document meta-data after creating the PdfWriter Instance. So move the highlighted codes below PdfWriter.getInstance.

Document document = new Document(PageSize.A4, -55, -65, 20, 7);
PdfWriter writer = null;
ByteArrayOutputStream byteOut = null;

byteOut = new ByteArrayOutputStream();                          
writer = PdfWriter.getInstance(document, byteOut);

document.addAuthor("pdfAuthor");
document.addCreator("pdfCreator");
document.addCreationDate();
document.addTitle("Title");
document.addSubject("Subject");
document.addKeywords("Keywords");

document.open();


Thursday, February 5, 2015

The viewer is unable to connect with the CrystalReportViewerServlet that handles asynchronous requests...

In the process of upgrading cr4je 11 to 12 version. Using crjava-runtime_12.2.218.jar inside my JavaEE Struts Application.

You would need to update your runtimes from the below link

http://scn.sap.com/docs/DOC-29757

Download 'SAP Crystal Reports for Java runtime components - Java Reporting Component (JRC) (~ 45 MB)'

Problem:

Managed to display the reports using ReportViewer jsp. However in the report with pagination, when I click next page an alert box showing this message.

The viewer is unable to connect with the CrystalReportViewerServlet that handles asynchronous requests. Please ensure that the Servlet and Servlet-Mapping have been properly declared in the application’s web.xml file

My ReportViewer jsp location:

   Webcontent
         + crystalreportviewers
         + common
              + reports
                   - ReportViewer.jsp
Web.xml:
<context-param>
      <param-name>crystal_document_view</param-name>
      <param-value>weblayout</param-value>
 </context-param>

 <context-param>
       <param-name>crystal_image_uri</param-name>
       <param-value>MyWebApp/crystalreportviewers</param-value>
 </context-param>

 <context-param>
        <param-name>crystal_image_use_relative</param-name>
        <param-value>webapp</param-value>
  </context-param>

  <servlet>
         <display-name>CrystalReportViewerServlet</display-name>  
         <servlet-name>CrystalReportViewerServlet</servlet-name>
         <servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
   </servlet>

   <servlet-mapping>
         <servlet-name>CrystalReportViewerServlet</servlet-name>
         <url-pattern>/CrystalReportViewerHandler</url-pattern>
    </servlet-mapping>


ReportViewer jsp:

    <%@ page import = "com.crystaldecisions.report.web.viewer.*"%>
    <%@ page import = "com.crystaldecisions.sdk.occa.report.data.*"%>
    <%@ page import = "com.crystaldecisions.sdk.occa.report.data.*"%>
   
   
   
 
   
    <%
         CrystalReportViewer viewer = new CrystalReportViewer();
         viewer.setName("Pending Report");

        //Enable Active-X print mode.
        viewer.setPrintMode(CrPrintMode.PDF);

        //Obtain report source of the report that the viewer will display. 
        Object reportSource = session.getAttribute("pendingReport");

        viewer.setReportSource(reportSource);

        Fields fields = (Fields) session.getAttribute("fields");
        viewer.setParameterFields(fields);
        viewer.setHasLogo(false); 
        viewer.setOwnPage(true);
        viewer.setDisplayGroupTree(false);
        viewer.setEnableDrillDown(true);  
        viewer.setBestFitPage(true);
        viewer.setURI("ReportActionClass.do?do=getPendingReport");
        viewer.setHasToggleGroupTreeButton(false);
         viewer.setHasViewList(false);
        viewer.setReuseParameterValuesOnRefresh(true);
        viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null);

    %>
   

   
   
Solution:

Problem get resolved if I move my report viewer jsp file to the same level as 'crystalreportviewers' folder and change context-param.

Report viewer location:

    Webcontent
           + crystalreportviewers
           - ReportViewer.jsp
web.xml  context param:

<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>MyWebApp/crystalreportviewers</param-value>
</context-param>


Alternate method:

I was in a situation to find an alternate way because  I don't have this issue in version 11. Moreover we have many reports at different folder levels.


So I've gone through the html source of created report viewer on both cases.(with problem & with solution)



If you check the generated source of crystal report viewer, you will find below code

Working condition (if crystal report viewer jsp is placed in the same level of \crystalreportviewers folder)



new bobj.crv.ViewerListener('AccountsPendingCallReport', new bobj.crv.ServletAdapter('CSAcctRewRpt.do?do=getPendingCallReport','CrystalReportViewerHandler'));

Otherwise you will see below (if crystal report viewer jsp is not placed in the same level of \crystalreportviewers folder)



new bobj.crv.ViewerListener('AccountsPendingCallReport', new bobj.crv.ServletAdapter('CSAcctRewRpt.do?do=getPendingCallReport','../../CrystalReportViewerHandler'));

The only difference is the second parameter of ServletAdapter constructor. Basically all user interactions (like navigation, export, download...) in crystal report are carried out by javascripts. So locate allInOne.js in \crystalreportviewers folder and find the below snippet where bobj.crv.ServletAdapter object been invoked.

Here I've replaced 'servletUrl' with "CrystalReportViewerHandler".

before
bobj.crv.ServletAdapter=function(pageUrl,servletUrl){this._pageUrl=pageUrl;this._servletUrl=servletUrl;this._form=null;}

after
bobj.crv.ServletAdapter=function(pageUrl,servletUrl){this._pageUrl=pageUrl;this._servletUrl="CrystalReportViewerHandler";this._form=null;}

This will fix the issue. In this way you may not need to place your report viewer jsp at same level as '\crystalreportviewers' folder.



Tuesday, June 24, 2014

java.sql.SQLException: ORA-01008: not all variables bound



One of the causes of this error can be found in the following snippet of code:

Cause:
query = "select * from AAA where condn = ?";
pstmt = con.prepareStatement(query);
pstmt.setString(1, firstValue);
// ResultSet rs = pstmt.executeQuery(query); // Wrong Usage - Do not provide query again
ResultSet rs = pstmt.executeQuery(); // Correct Usage

Another reason is that you have not mapped all '?' with corresponding values

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal


Problem:

package foo;

import java.math.BigDecimal;

public class BigDecimalTest {

   public static void main(String[] args) {

       BigDecimal foo = new BigDecimal("1");
       BigDecimal bar = new BigDecimal("125.4");

       
       BigDecimal result = foo.divide(bar);
       
       System.out.println(foo + " / " + bar + " = " + result);

   }
}

The above example will result in a  

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.. BigDecimal's divide() method uses a really effed up way to calculate the scale of the result: dividend.scale() - divisor.scale(). In order for this to work, you have to use one of the other divide() methods, where you can add another parameter specifying the scale or a predefined MathContext. Mh, I just noticed that the divide() method in question was added in 1.5, in 1.4 all divide() methods required a scale or RoundingMode.
As I see it, the new version of the method is pretty much useless,
since there's always the chance of breaking at runtime when you don't
have control over the values, e. g. if read from a database. Seems like
they tried to fix a class that's broken beyond all repair.

So a working version of the above example would look like this:


package foo;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalTestScale {

   public static void main(String[] args) {

       BigDecimal foo = new BigDecimal("1");
       BigDecimal bar = new BigDecimal("125.4");

       
       BigDecimal result = foo.divide(bar, 3, BigDecimal.ROUND_HALF_UP);

       
       System.out.println(foo + " / " + bar + " = " + result);

}

Reference:

java.net.SocketException: Connection reset by peer


The server is reporting "connection reset by peer" in the middle of a request. That means one of the following things:

  • The browser started a request and the user pressed STOP.

  • The browser started an request in one frame and JavaScript on another frame told it to go somewhere else (which has the same effect as a user pressing STOP).

  • You have a meta refresh reload someplace that is firing so fast that the previous request hasn't had a chance to complete yet. The five second periodicity makes me pretty suspicious of this.

  • You have some network component in between (a router or proxy) that is misbehaving.

  • You have some completely separate connection attempts going on that are disobeying the HTTP protocol (such as doing a telnet connection to port 8080 and then disconnecting).

Error message in the HTTP Server error.log: File does not exist:

Problem Description:
I have deployed an webapplication on Websphere Application server 6.0.2.15. This runs on port 9080.
I have also deployed HTTP server and the plugin and are planning to use the HTTP Server as a proxy for the WAS server.
HTTP server is running on port 8080.

When trying to open following url: http://localhost:8080/myapp/signin.do I am supposed to be forwarded to the Application server where the myapp resources resides. This is not happening. Seems like the HTTP server is looking on its own document root instead of doing the actual forward.
Error message in the HTTP Server error.log:
File does not exist: D:/IBM HTTP Server/htdocs

Solution:
Solution in my case was to manually set the port 8080 in the WAS admin console:
Environment -> Virtual Hosts -> Default host -> Host Aliases

Servlet Custom error page not shown


The Servlet 2.2 specification allows you to specify an error page (a servlet or a JSP) for different kinds of HTTP errors or ServletExceptions. You can specify this in deployment descriptor of the web application as:

<error-page>
   <exception-type>FooException</exception-type>
   <location>/error.jsp</location>
 </error-page>

where FooException is a subclass of ServletException.
The web container invokes this servlet in case of errors, and you can access the following information from the request object of error servlet/JSP: error code, exception type, and a message. Refer to section 9.8 of Servlet 2.2 spec.

Here is the document:

 
<?xml version="1.0"?>

<!DOCTYPE web-app

   PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

   "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<error-page>

   <error-code>404</error-code>

   <location>/error404.jsp</location>

</error-page>

</web-app>

Problem:

The problem I am experiencing is that Internet Explorer refuses to show my error page and shows his.
I tried with different browsers (Mozilla, Opera) and the output of the 404.jsp page is nicely shown.

Solution 1: (Client Side)

Explorer catches the HTTP message code 404 and replaces all the content with its own error page.

To switch this feature off go to:
Tools -> Internet Options... -> Advanced.

Then look for "Show friendly HTTP error messages" and uncheck it. Click OK.

Solution 2: (Working Solution)

The custom error pages are catched and replaced if they are "too small".

Looks like that Explorer thinks your "small" page is too ugly and so decides to show his.

If the page weights a few Kb then it is shown: so you can either create
a nice looking page or you can add some extra spaces to you page to
meet the Explorer's expectations.


Solution 3:

try setting the status to 200 in your error jsp.
 
<% response.setStatus(200); %>
This should tell the browser that the request was valid and it will show your page instead of the 'Friendly Error Page'.