Question

I am sending XML data as content type text/xml via a POST to an XPage, but I am unable to locate the data from within the XPage. param.ToString() just returns "{}". param will return parameters in the URL, however. This is the sever-side javascript in the afterRenderResponse event for the Xpage (rendered="false"):

var extCont = facesContext.getExternalContext(); 
var pageOutput = facesContext.getResponseWriter(); 
var pageResponse = extCont.getResponse(); 
pageResponse.setContentType("text/xml"); 
pageResponse.setHeader("Cache-Control", "no-cache"); 

//Get data from POST
var data = param.toString();
//echo back what was POSTed
pageOutput.write(data);

pageOutput.endDocument(); 
facesContext.responseComplete(); 

All that is returned for var data is "{}" How do I access the XML data that was posted?

The XML data was posted via a LotusScript agent that does work when I have used it to post to another API (not developed in Notes):

xmldata = "<tag>TEST</tag>"
Set httpObject = CreateObject("MSXML2.ServerXMLHTTP")
httpURL = baseURL + "/echoxml.xsp"
Call httpObject.open("POST", httpURL, False)
Call httpObject.setRequestHeader("Content-Type", "text/xml")
Call httpObject.send( xmldata )
httpStatus = httpObject.Status
Print "Status = "+CStr(httpStatus)

I do get a status code of 200 indicating a successful POST.

Where is the data in XPages? Thanks in advance.

Was it helpful?

Solution

you might want to change your local JS code to be browser neutral :-) That code works in IE only. (But that's not the problem at hand). A XPage is a life-cycle managed server construct and isn't designed to process arbitrary data thrown at it.

The recommended way to deal with what you want to do is to use the REST control from the extension library. You can configure a URL part for it e.g. data, so you would post to baseURL + "/echoxml.xsp/data". There you have the payload readily in a variable.

The Rest control also supports the use of an custom Java class if you prefer to do that.

The last option is to add a custom servlet. Here are the steps:

  1. Open the Navigator view and create /Code/Java/META-INF/services/com.ibm.xsp.adapter.servletFactory as text file (that exact name)
  2. add one line with the name of your servlet. eg. com.acme.EchoFactory
  3. Write the echo factory class:

    public class EchoFactory implements IServletFactory{
    private ComponentModule module;
    
    public ServletMatch getServletMatch(final String contextPath, final String path) throws ServletException {
    
        if (path.startsWith("/xsp/echo")) {
            return new ServletMatch(module.createServlet("com.acme.EchoServlet", "Can you hear it?", null),"",path);
        }
    
        return null;
        }
    
    public void init(final ComponentModule module) {
        this.module = module;    
        }
    }
    
  4. Implement the Echo Servlet

       public class EchoServlet extends DesignerFacesServlet {
           @Override
           public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                HttpServletRequest req = (HttpServletRequest) servletRequest;
                HttpServletResponse res = (HttpServletResponse) servletResponse;
                FacesContext facesContext = this.getFacesContext(req, res);
                        String method = req.getMethod();
                        // Your code goes here!
               }
    

Hope that helps

OTHER TIPS

Stephen is right, that xpages aren't necessarily meant to do what you are wanting, but they certainly can. What you need to do to get a handle on the xml is use the getInputStream method using the code below, that is where the POSTed data should be going. Once you have the InputStream you can read it back into a file or display it whatever you want.

((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getInputStream()

Regarding stwissel answer, the downside with the REST control from the extension library is that doesn't respect the HTTP Headers set by the Server, so is not useful for CORS requests, so is not useful for hybrid mobile applications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top