Creating a Web service response JSP page

In this tutorial lesson, you are defining the Web service response JSP pages. When your Web service executes, either a successful execution or a failure of the operation response returns. In this lesson, you are creating the template for these success and a failure message.

About this task

When executing the business logic, there are two types of responses that can be returned. The responses comply with the OAGIS ConfirmBOD schema definition. One response is used to show a successful execution of the service operation while the other represents a failure of the service operation. In a successful operation, the confirmation response would return the internal WebSphere Commerce identifier of the customer. In a failure operation, the confirmation indicates the error for why the request failed. In this section, the process of defining both JSP templates is described.

The ConfirmBOD schema is verbose and the result of this service request does not require most of the elements in this schema. It is recommended to use the Rational XML tooling to generate a template for success and failure message. By using the tooling, a skeleton of the JSP template can be created to contain only the information from each response.

The following steps demonstrate how to create the ConfirmBOD template for successful execution with the Rational XML Tooling.

Procedure

  1. Create a MyCompany folder in the Stores project, if one does not exist:
    1. In the Enterprise Explorer view, navigate to Stores/WebContent/webservices.
    2. Right-click the webservices folder; select New > Folder.
    3. Name the folder MyCompany.
  2. Generate a ConfirmSyncCustomerSuccessBOD.xml XML file template:
    1. Navigate to WebServicesRouter/WebContent/xsd/OAGIS/9.0/BODs/Developer.
    2. Right-click the ConfirmBOD.xsd file; select Generate > XML File.
    3. In the New XML File dialog, for the Enter or select the parent folder, enter Stores/WebContent/webservices/MyCompany as the parent folder.
    4. For the File name, enter ConfirmSyncCustomerSuccessBOD.xml as the name.
    5. Click Next.
    6. In the Select Root Element dialog, for the Root element, select ConfirmBOD.
    7. For the Namespace Information, select the default Prefix p; click the Edit... button.
    8. In the New Namespace Information dialog, For the Prefix, enter oa. Click OK.
    9. Ensure the options to Create optional attributes or Create optional elements are not selected.
    10. Click Finish to create the XML template.
  3. Repeat step 2 to create a ConfirmSyncCustomerErrorBOD.xml XML template. This template is used when the Web service operation fails.
  4. Edit the ConfirmSyncCustomerSuccessBOD.xml file to add the elements to be included with a successful confirmation of a service request.
    You are creating the following XPath attributes and elements to the success confirmation:
    ConfirmBOD/DataArea/BOD/BODSuccessMessage/UserArea/ReviewerContact/ID
    ConfirmBOD/DataArea/BOD/BODSuccessMessage/UserArea/ReviewerContact/Name
    
    1. Navigate to Stores/WebContent/webservices/MyCompany.
    2. Right-click the ConfirmSyncCustomerSuccessBOD.xml file; select Open With > XML Editor.
    3. In the Outline view, expand oa:ConfirmBOD/oa:DataArea/oa:BOD.
    4. Right-click oa:BOD; select Add Child > New Element. Enter oa:BODSuccessMessage for the element name.
    5. Right-click oa:BODSuccessMessage; select Add Child > New Element. Enter oa:UserArea for the element name.
    6. Right-click oa:UserArea; select Add Child > New Element. Enter oa:ReviewerContact for the element name.
    7. Right-click oa:ReviewerContact; select Add Child > New Element. Enter oa:ID for the element name.
    8. Right-click oa:ReviewerContact; select Add Child > New Element. Enter oa:Name for the element name.
    9. Save your changes and close the file.
  5. Edit the ConfirmSyncCustomerErrorBOD.xml file to add the elements to be included with a failure confirmation of a service request:
    You are creating the following XPath attributes and elements to the success confirmation:
    ConfirmBOD/DataArea/BOD/BODFailureMessage/ErrorProcessMessage/ID
    ConfirmBOD/DataArea/BOD/BODFailureMessage/ErrorProcessMessage/Description
    ConfirmBOD/DataArea/BOD/BODFailureMessage/ErrorProcessMessage/Note
    ConfirmBOD/DataArea/BOD/BODFailureMessage/ErrorProcessMessage/Type
    ConfirmBOD/DataArea/BOD/BODFailureMessage/ErrorProcessMessage/ReasonCode
    
    1. Right-click the ConfirmSyncCustomerErrorBOD.xml file; select Open With > XML Editor.
    2. In the Outline view, expand oa:ConfirmBOD/oa:DataArea/oa:BOD.
    3. Right-click oa:BOD; select Add Child > New Element. Enter oa:BODFailureMessage for the element name.
    4. Right-click oa:BODFailureMessage; select Add Child > New Element. Enter oa:ErrorProcessMessage for the element name.
    5. Right-click oa:ErrorProcessMessage; select Add Child > New Element. Enter oa:ID for the element name.
    6. Repeat steps 5.e. to create the XML elements oa:Description, oa:Note, oa:Type, and oa:ReasonCode.
    7. Save your changes and close the file.
  6. Rename ConfirmSyncCustomerSuccessBOD.xml and ConfirmSyncCustomerErrorBOD.xml with a .jsp extension instead of the .xml extension. Your modified files are now named ConfirmSyncCustomerSuccessBOD.jsp and ConfirmSyncCustomerErrorBOD.jsp
  7. Add the JSP code to ConfirmSyncCustomerSuccessBOD.jsp to represent the result of the Web service operation. For the success confirmation, add the user identifier and distinguish name in the review id and name elements.
    1. Right-click ConfirmSyncCustomerSuccessBOD.jsp; select Open.
    2. Click the Source tab.
    3. Remove the <?xml..?> tag from the JSP because the template XML is appended to the SOAP response and this tag invalidates the resulting XML response.
    4. Add the following code to the beginning of your JSP:
      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      
    5. Add the user identifier and distinguish name in the review ID and Name elements.
      <oa:ID><c:out value="${CommandContext.userId}" /></oa:ID>
      <oa:Name><c:out value="${CommandContext.user.distinguishedName}" /></oa:Name>
      
    6. Ensure that your JSP code resembles the following code:
      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      <oa:ConfirmBOD releaseID=""
            xmlns:clm54217="http://www.openapplications.org/oagis/9/currencycode/54217:2001" 
            xmlns:clm5639="http://www.openapplications.org/oagis/9/languagecode/5639:1988" 
            xmlns:clm66411="http://www.openapplications.org/oagis/9/unitcode/66411:2001" 
            xmlns:clmIANAMIMEMediaTypes="http://www.openapplications.org/oagis/9/IANAMIMEMediaTypes:2003" 
            xmlns:oa="http://www.openapplications.org/oagis/9" 
            xmlns:p1="http://www.openapplications.org/oagis/9/codelists" 
            xmlns:qdt="http://www.openapplications.org/oagis/9/qualifieddatatypes/1.1" 
            xmlns:udt="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.openapplications.org/oagis/9 ../../../../WebServicesRouter/WebContent/xsd/OAGIS/9.0/BODs/Developer/ConfirmBOD.xsd ">
            <oa:ApplicationArea>
                  <oa:CreationDateTime>oa:CreationDateTime</oa:CreationDateTime>
            </oa:ApplicationArea>
            <oa:DataArea>
                  <oa:Confirm />
                  <oa:BOD>
                        <oa:OriginalApplicationArea>
                              <oa:CreationDateTime>oa:CreationDateTime</oa:CreationDateTime>
                        </oa:OriginalApplicationArea>
                        <oa:BODSuccessMessage>
                              <oa:UserArea>
                                    <oa:ReviewerContact>
                                          <oa:ID><c:out value="${CommandContext.userId}" /></oa:ID>
                                          <oa:Name><c:out value="${CommandContext.user.distinguishedName}" /></oa:Name>
                                    </oa:ReviewerContact>
                              </oa:UserArea>
                        </oa:BODSuccessMessage>
                  </oa:BOD>
            </oa:DataArea>
      </oa:ConfirmBOD>
      
  8. Add the JSP code to ConfirmSyncCustomerErrorBOD.jsp to represent the result of the Web service operation.
    1. Right-click ConfirmSyncCustomerErrorBOD.jsp; select Open.
    2. Remove the <?xml..?> tag from the JSP because the template XML is appended to the SOAP response and this tag invalidates the resulting XML response.
    3. Add the error information to the beginning of your JSP to associate the business operation to the ErrorProcessMessage elements.
      <%
            com.ibm.commerce.beans.ErrorDataBean errorBean = new com.ibm.commerce.beans.ErrorDataBean ();
            com.ibm.commerce.beans.DataBeanManager.activate(errorBean, request, response);
      
            String id = errorBean.getECMessage().getSystemMessageIdentifier();
            String reasonCode = errorBean.getErrorCode();
            String type = errorBean.getExceptionType();
            String note = errorBean.getCorrectiveActionMessage();
            
            String message = errorBean.getSystemMessage();
            if(message == null || message.trim().length() == 0) {
                  message = errorBean.getMessage();
            }
      %>
      
    4. Add the values for your ErrorProcessMessage elements:
       <oa:ID><%= id %></oa:ID>
      <oa:Description><%= message %></oa:Description>
      <oa:Note><%= note %></oa:Note>
      <oa:Type><%= type %></oa:Type>
      <oa:ReasonCode><%= reasonCode %></oa:ReasonCode>
      
    5. Ensure that your JSP code resembles the following code:
      <%
            com.ibm.commerce.beans.ErrorDataBean errorBean = new com.ibm.commerce.beans.ErrorDataBean ();
            com.ibm.commerce.beans.DataBeanManager.activate(errorBean, request, response);
      
            String id = errorBean.getECMessage().getSystemMessageIdentifier();
            String reasonCode = errorBean.getErrorCode();
            String type = errorBean.getExceptionType();
            String note = errorBean.getCorrectiveActionMessage();
            
            String message = errorBean.getSystemMessage();
            if(message == null || message.trim().length() == 0) {
                  message = errorBean.getMessage();
            }
      %>
      <oa:ConfirmBOD releaseID=""
            xmlns:ao="http://www.openapplications.org/oagis/9" 
            xmlns:clm54217="http://www.openapplications.org/oagis/9/currencycode/54217:2001" 
            xmlns:clm5639="http://www.openapplications.org/oagis/9/languagecode/5639:1988" 
            xmlns:clm66411="http://www.openapplications.org/oagis/9/unitcode/66411:2001" 
            xmlns:clmIANAMIMEMediaTypes="http://www.openapplications.org/oagis/9/IANAMIMEMediaTypes:2003" 
            xmlns:p1="http://www.openapplications.org/oagis/9/codelists" 
            xmlns:qdt="http://www.openapplications.org/oagis/9/qualifieddatatypes/1.1" 
            xmlns:udt="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.openapplications.org/oagis/9 ../../../../WebServicesRouter/WebContent/xsd/OAGIS/9.0/BODs/Developer/ConfirmBOD.xsd ">
            <oa:ApplicationArea>
                  <oa:CreationDateTime>oa:CreationDateTime</oa:CreationDateTime>
            </oa:ApplicationArea>
            <oa:DataArea>
                  <oa:Confirm />
                  <oa:BOD>
                        <oa:OriginalApplicationArea>
                              <oa:CreationDateTime>oa:CreationDateTime</oa:CreationDateTime>
                        </oa:OriginalApplicationArea>
                        <oa:BODFailureMessage>
                              <oa:ErrorProcessMessage>
                                    <oa:ID><%= id %></oa:ID>
                                    <oa:Description><%= message %></oa:Description>
                                    <oa:Note><%= note %></oa:Note>
                                    <oa:Type><%= type %></oa:Type>
                                    <oa:ReasonCode><%= reasonCode %></oa:ReasonCode>
                              </oa:ErrorProcessMessage>
                        </oa:BODFailureMessage>
                  </oa:BOD>
            </oa:DataArea>
      </oa:ConfirmBOD>
      

Results

You have created the JSP templates used to represent the SyncCustomer web service. The final step required for creating the web service is to register the JSP as shown in the next section.