Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Few days back I saw a discussion on the custom error when iview times out. There may be n number of reasons for the time out, depending on the type of iview and the manner in which it is displayed. One such scenario may occur with HTML pages, which I have tried to handle in the following example.

Scenario: There is some static content/text which may change very frequently and hence you decide to use HTML file to display that iview rather than using portal application.

Solution: You render the content via html file and when the file is unable to load the content due to any reason, the custom error message is displayed on the user screen plus an email is sent to the admin, notifying him about the error on the page.

Step 1:

Create a portal application project and define an interface in it. Let’s name it as ICommonServices. Import SAPs IService, extend it in your interface and initialize your methods like below.

import com.sapportals.portal.prt.service.IService;

public interface ICommonServices   extends IService

{

    public abstract String getHTMLOutput(String s, String s1);

    public abstract void sendErrorEmail(String s, String s1);

    public abstract void setFromEmailAddress(String s);

    public abstract void setToEmailAddress(String s);

    public abstract void setEmailSubject(String s);

    public abstract void setEmailBody(String s);

    public abstract int sendEmail();

    public static final String KEY = “Define_Some_Key”;

}

Step 2:

Now create a class in the same package. Let’s name this class as MyCommonService, implement your ICommonServices and define your methods here. Use the below code for the same.

    private IServiceContext mm_serviceContext;

    private static final String strfinalErrorMsg = "<b>There is an Error! Mail has been sent to the System Administrator</b>";

    private final String strErrorHtmlSource = "ErrorPage.html";

    private String outputcontent;

    private String ErrSource;

    private String EmailFrom;

    private String EmailTo;

    private String EmailSubj;

    private String EmailBody;

    private boolean errException;

    private String ErrMsg;

//First comes the constructor

public MyCommonService()

    {

        outputcontent = "";

        ErrSource = "";

        EmailFrom = "";

        EmailTo = "";

        EmailSubj = "";

        EmailBody = "";

        errException = false;

        ErrMsg = "";

    }

    public void init(IServiceContext serviceContext)

    {

        mm_serviceContext = serviceContext;

    }

    public void afterInit()

    {

    }

    public void destroy()

    {

    }

    public IServiceContext getContext()

    {

        return mm_serviceContext;

    }

    public String getKey()

    {

        return "Use_same_Key_Defined_Earlier";

    }

    public void setFromEmailAddress(String EmailFrom)

    {

        this.EmailFrom = EmailFrom;

    }

    public void setToEmailAddress(String EmailTo)

    {

        this.EmailTo = EmailTo;

    }

    public void setEmailSubject(String EmailSubj)

    {

        this.EmailSubj = EmailSubj;

    }

    public void setEmailBody(String EmailBody)

    {

        this.EmailBody = EmailBody;

    }

    public int sendEmail()

    {

        return send() ? 0 : -1;

    }

//Define a send Method

    private boolean send()

    {

        errException = false;

        Properties props = new Properties();

        props.put("mail.smtp.host", "your_SMTP_add");

        Session session = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(session);

        InternetAddress addressFrom = new InternetAddress(EmailFrom);

        msg.setFrom(addressFrom);

        InternetAddress addressTo = new InternetAddress(EmailTo);

        msg.setRecipient(javax.mail.Message.RecipientType.TO, addressTo);

        msg.setSubject(EmailSubj);

        msg.setContent(EmailBody, "text/html");

        msg.setSentDate((new GregorianCalendar()).getTime());

        Transport.send(msg);

        return errException;

        AddressException ae;

         ae;

        sendErrorEmail("FullName_Of_this_parFile", ae.toString());

        errException = true;

        ae.printStackTrace();

        return errException;

        MessagingException me;

          me;

        sendErrorEmail("FullName_Of_this_parFile ", me.toString());

        errException = true;

        me.printStackTrace();

        return errException;

        Exception e;

          e;

        sendErrorEmail("FullName_Of_this_parFile ", e.toString());

        errException = true;

        e.printStackTrace();

        return errException;

        Exception exception;

        return errException;

    }

//Define error email skeleton

    public void sendErrorEmail(String ErrSrc, String ErrMsg)

    {

        setFromEmailAddress("SystemMonitor@atul.com");

        setToEmailAddress("atul@sap.com");

        setEmailSubject("Do not Reply");

        setErrorEmailBody(ErrSrc, ErrMsg);

        boolean i = send();

    }

//Define email body

  private void setErrorEmailBody(String ErrSource, String ErrMsg)

    {

        StringBuffer tmpBodyString = new StringBuffer();

        tmpBodyString.append("<html>");

tmpBodyString.append("<head><title>Error Message  !!</title></head>");

tmpBodyString.append("<body>");

        tmpBodyString.append("Hi Administrator,");

tmpBodyString.append("<BR>");

        tmpBodyString.append("The following error occured : ");

tmpBodyString.append("<BR><BR>");

tmpBodyString.append("<b>Source of Error:</b> ");

        tmpBodyString.append(ErrSource);

tmpBodyString.append("<BR>");

        tmpBodyString.append("<b>Error Message:</b> <BR>");

        tmpBodyString.append(ErrMsg);

tmpBodyString.append("<BR><BR>");

tmpBodyString.append("</body>");

tmpBodyString.append("</html>");

        EmailBody = tmpBodyString.toString();

    }

//This method is to accept the incoming URL and generate html

     private String readUrlAndWriteHtml(String URLSrc)

    {

        String output;

        String content_dest = "";

        outputcontent = "";

        output = "";

        errException = false;

        URL sourceHtmlUrl = new URL(URLSrc);

        URLConnection URLConn = sourceHtmlUrl.openConnection();

        BufferedReader inBR = new BufferedReader(new InputStreamReader(URLConn.getInputStream()));

        StringBuffer strBuf = new StringBuffer();

        while((content_dest = inBR.readLine()) != null)

            strBuf.append(content_dest);

        output = strBuf.toString();

        inBR.close();

        return output;

        MalformedURLException MUE;

MUE;

                sendErrorEmail(URLSrc, MUE.toString());

                errException = true;

                return output;

        IOException IOE;

IOE;

                 sendErrorEmail(URLSrc, IOE.toString());

                errException = true;

                 return output;

        Exception E;

E;

                sendErrorEmail(URLSrc, E.toString());

                 errException = true;

                 return output;

        Exception exception;

        return output;

    }

//Get the HTML output from this method

    public String getHTMLOutput(String StrPath, String SourceHtml)

    {

        String strcontentout = "";

        StrPath = "http://localhost:####";

       strcontentout = readUrlAndWriteHtml(StrPath + SourceHtml);

        if(errException)

        {

            strcontentout = readUrlAndWriteHtml(StrPath + "ErrorPage.html");

            return "<b>There is an Error! Mail has been sent to the System Administrator</b>";

        }

        return strcontentout;

    }

Step 3:

In the portalapp.xml, add sharing reference property for mail library under application config.

<property name="SharingReference" value="SAPJ2EE::library:mail" />

Step 4:

Define Error.html and place it on your server

Step 5:

Create another portal application project and an object to it. In your jsp, import your interface ICommonServices along with com.sapportals.portal.prt.runtime.IPortalRuntimeResources, com.sapportals.portal.prt.runtime.PortalRuntime and com.sapportals.portal.prt.service.IService. Use the below scriplet to call your HTML content.

<%

                IPortalRuntimeResources runtimeResources = PortalRuntime.getRuntimeResources();

                IService aService = runtimeResources.getService("You_Par_file_Name_comes_here");

                ICommonServices myService = (ICommonServices) aService;

                // get the current server Path which includes portal, server domain & port number.       

                String rootPath = request.getRequestURL().toString().replaceFirst(request.getRequestURI(),"");

                //set the requested source of html file in the virtual location.

                String htmlSource="MyContent.html";

                //call service method and display the output

                String strDisplay=myService.getHTMLOutput(rootPath, htmlSource);

                 out.print(strDisplay);

%>

Step 6:

In its portalapp.xml, give reference to your services par file

<property name="SharingReference" value=" You_Par_file_Name_comes_here " />

Step 7:

Define MyContent.html and place it at your server.

Step 8:

Export both the above par files on to your server. Create an iview for the above portal application and use this on any page.

Conclusion:

The HTML will be rendered in the iview and in case of error, admin will be notified of the exact error via email and the custom error message will be displayed at the screen for the end users.

Labels in this area