Skip to Content
Author's profile photo Tal Haviv

How to modify enterprise portal response HTML sections and response headers

Hi,

In this blog you can find 2 topics:

  1. Adding response headers to the response coming back from EP server.
  2. Modifying the HTML head section (or any other section in html) of the HTML response coming back from EP server.

1. Adding HTTP Response headers in Enterprise Portal

 

What is an HTTP Header (wikipedia):

HTTP header fields are components of the header section of request and response messages in the Hypertext Transfer Protocol (HTTP). They define the operating parameters of an HTTP transaction.

 

Sometimes there are cases where you would like to add headers to response. some example can be X-FRAME-OPTIONS for limiting framing or Cache-Control to control caching of the response, or  IE’s X-UA-Compatible

 

Here is sample code of a portal component which adds adds an X-FRAME-OPTIONS response header to deny framing when called:

import javax.servlet.http.HttpServletResponse;
import com.sapportals.portal.prt.component.AbstractPortalComponent;
import com.sapportals.portal.prt.component.IPortalComponentContext;
import com.sapportals.portal.prt.component.IPortalComponentProfile;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentResponse;
public class MyHeaderComponent extends AbstractPortalComponent
{
    private static final String X_FRAME_OPTIONS = "X-Frame-Options"; //The response header key
    private static final String DENY = "DENY";  //The response header value
    public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
    {
            HttpServletResponse servletResponse = request.getServletResponse(false); //gets the original servlet response
            if(servletResponse != null) {
     
               //adds the X-FRAME-OPTIONS Header
                servletResponse.addHeader(X_FRAME_OPTIONS, DENY);                   
                response.write("My Content cannot be displayed inside an iframe!!");
            }
   }
} 



The actual magic is done in line 19.

The response of calling this portal component would be display just “My Content cannot be displayed in an iframe”.

If you run it within IE inside an Iframe, you will get the following:

/wp-content/uploads/2014/09/frame_551158.jpg

Notice the response header that was added : “x-frame-options” .

2. Modifing the HTML head and html sections of EP response:

 

Simple structure of an HTML page.

As you know, when working with portal components, Portal Runtime builds and creates an html response that will return to client (after going over all hooks).

It is possible to add additional html code(or remove) to the head or body sections and to change attributes of these sections.

 

At first we need to have access to the portal HTMLDocument which will allow us access to the html document sections:

 

/** Getting the PRT HtmlDocument object from the PortalComponentRequest. */
private HtmlDocument getHtmlDocument(IPortalComponentRequest request) {
     HtmlDocument htmlDocument = null;
     IPortalResponse portalResponse = (IPortalResponse) request.getValue(IPortalResponse.class.getName());
      if (portalResponse instanceof PortalHtmlResponse) {
           PortalHtmlResponse portalHtmlResponse = (PortalHtmlResponse) portalResponse;
           htmlDocument = portalHtmlResponse.getHtmlDocument();
      }
     return htmlDocument;
  }

Then in our doContent of portal component we can just add scripts to the head and play with the body:

 

public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
{
     HtmlDocument portalHtmlDoc = getHtmlDocument(request);
     Vector headHtmlElements = portalHtmlDoc.getHead().getHtmlElements();
     //writing head scripts and modifying body attribs
     headHtmlElements.add( new HtmlString( "<!-- This will appear in the head! --> " ));
     headHtmlElements.add( new HtmlString( "<script type=\"text/javascript\">alert('hello from header!') </script>" ));
  
     //setting css class of body
     portalHtmlDoc.getBody().setClass( "myCSSClass" );
     
     //setting body attributs
     portalHtmlDoc.getBody().addAttribute( "role", "application" );       
     response.write("inside body");
}

After we got the portal HTML Document, adding some html code inside the head section is done in line 7,8.

Setting class for the body section in line 11 and adding body attributes in line 14.

Finally we can still write inside the body in line 15.

Running this portal component will trigger our javascript alert and give us the following html output:

/wp-content/uploads/2014/09/head_551246.jpg

You can see a generic portal html response, but notice the additional changes we have added in our code:

  • 2 lines we wrote were added into the html head section (marked in read).
  • The body section now has role=”application and class=”myCSSClass” (marked in orange)

Inspect the getHead and getBody classes for more methods.

Try it out!

Best Regards,

Tal

You want to influence the future product roadmap of Enterprise Portal? You plan to implement your portal in the Cloud? Then you should participate in the following survey https://blogs.sap.com/2016/10/11/2016-elections-vote-now/.

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Niladri Chakravarty
      Niladri Chakravarty

      hi,

      I don't know much about this, can you please help me to find the Enterprise Portal Response.html page for doing any modification.

      I am using EP 7.1

      Author's profile photo Tal Haviv
      Tal Haviv
      Blog Post Author

      Hi Niladri,

      Any portal component that you have generates an HTML response that eventually be written back to browsers.

      If, in your portal component, you want to add/modify html headers - this is the way to do it.

      Does that help a bit?

      Try to be specific on what exactly you need.

      thanks,

      Tal

      Author's profile photo Mehmet Ali Subasi
      Mehmet Ali Subasi

      Hello, I need to set response header to my portal application ( sapui5 app). I don't understand where I am going to write this code? Thank you, regards.

      Author's profile photo Former Member
      Former Member

      Hi Tal-

      We need to add response headers to Portal response on page https://<ServerName>/irj/portal. Where can we find the response file for /irj/portal. I did not find anything at location /usr/sap/<SID>/J00/j2ee/cluster/apps/sap.com/irj/servlet_jsp/irj/root.


      Is there an easy way to find the response page for /irj/portal ?

      Thanks

      -Amit

      Author's profile photo Tal Haviv
      Tal Haviv
      Blog Post Author

      Hi,

      irj/portal is the main portal url ,eventually it runs the portal framework page (which has several iviews based on several portal components. These are TLN (Top level Navigation), and other.

      You can add another iView to the framework page and it will run when navigation to /irj/portal.

      Author's profile photo Javier Nathan Ortiz
      Javier Nathan Ortiz

      Hello,

      I follow the example, but I cannot find the class PortalHtmlResponse. Where can i find it?(DC,jar)

      Regards,

      Nathan

      Author's profile photo Tal Haviv
      Tal Haviv
      Blog Post Author

      This is in \j2ee\cluster\apps\sap.com\com.sap.portal.runtime.system.connection\servlet_jsp\com.sap.portal.runtime.system.connection\root\WEB-INF\lib\com.sap.portal.runtime.system.connection_api.jar

      Author's profile photo Javier Nathan Ortiz
      Javier Nathan Ortiz

      Thank you. It worked.

      Author's profile photo Former Member
      Former Member

      Hi Tal,

      I have implemented your solution but unfortunately it didn't work in SAP Nw 7.0 as well as in SAP NW 7.4.I am getting null value of portalHtmlDoc .

      I have also checked logs in nwa and getting following error:

      java.lang.NullPointerException at com.nokia.FirstComp.getHtmlDocument(FirstComp.java:37)

      Jar files added in Project are:

      1.com.sap.portal.runtime.system.connection_api

      2.prtapi.jar

      3.prtconnectionjar

       

      Could you please help here?