Esempio n. 1
0
  private void createAndSendSOAPResponse(
      Map<String, Object> serviceResults, String serviceName, HttpServletResponse response)
      throws EventHandlerException {
    try {
      // setup the response
      Debug.logVerbose("[EventHandler] : Setting up response message", module);
      String xmlResults = SoapSerializer.serialize(serviceResults);
      // Debug.logInfo("xmlResults ==================" + xmlResults, module);
      XMLStreamReader reader =
          XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
      StAXOMBuilder resultsBuilder = new StAXOMBuilder(reader);
      OMElement resultSer = resultsBuilder.getDocumentElement();

      // create the response soap
      SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
      SOAPEnvelope resEnv = factory.createSOAPEnvelope();
      SOAPBody resBody = factory.createSOAPBody();
      OMElement resService = factory.createOMElement(new QName(serviceName + "Response"));
      resService.addChild(resultSer.getFirstElement());
      resBody.addChild(resService);
      resEnv.addChild(resBody);

      // The declareDefaultNamespace method doesn't work see
      // (https://issues.apache.org/jira/browse/AXIS2-3156)
      // so the following doesn't work:
      // resService.declareDefaultNamespace(ModelService.TNS);
      // instead, create the xmlns attribute directly:
      OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
      resService.addAttribute(defaultNS);

      // log the response message
      if (Debug.verboseOn()) {
        try {
          Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
        } catch (Throwable t) {
        }
      }

      resEnv.serialize(response.getOutputStream());
      response.getOutputStream().flush();
    } catch (Exception e) {
      Debug.logError(e, module);
      throw new EventHandlerException(e.getMessage(), e);
    }
  }
Esempio n. 2
0
  /**
   * @see org.ofbiz.webapp.event.EventHandler#invoke(ConfigXMLReader.Event,
   *     ConfigXMLReader.RequestMap, javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  public String invoke(
      Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response)
      throws EventHandlerException {
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");

    // first check for WSDL request
    String wsdlReq = request.getParameter("wsdl");
    if (wsdlReq == null) {
      wsdlReq = request.getParameter("WSDL");
    }
    if (wsdlReq != null) {
      String serviceName = RequestHandler.getOverrideViewUri(request.getPathInfo());
      DispatchContext dctx = dispatcher.getDispatchContext();
      String locationUri = this.getLocationURI(request);

      if (serviceName != null) {
        Document wsdl = null;
        try {
          wsdl = dctx.getWSDL(serviceName, locationUri);
        } catch (GenericServiceException e) {
          serviceName = null;
        } catch (WSDLException e) {
          sendError(response, "Unable to obtain WSDL", serviceName);
          throw new EventHandlerException("Unable to obtain WSDL", e);
        }

        if (wsdl != null) {
          try {
            OutputStream os = response.getOutputStream();
            response.setContentType("text/xml");
            UtilXml.writeXmlDocument(os, wsdl);
            response.flushBuffer();
          } catch (IOException e) {
            throw new EventHandlerException(e);
          }
          return null;
        } else {
          sendError(response, "Unable to obtain WSDL", serviceName);
          throw new EventHandlerException("Unable to obtain WSDL");
        }
      }

      if (serviceName == null) {
        try {
          Writer writer = response.getWriter();
          StringBuilder sb = new StringBuilder();
          sb.append("<html><head><title>OFBiz SOAP/1.1 Services</title></head>");
          sb.append("<body>No such service.").append("<p>Services:<ul>");

          for (String scvName : dctx.getAllServiceNames()) {
            ModelService model = dctx.getModelService(scvName);
            if (model.export) {
              sb.append("<li><a href=\"")
                  .append(locationUri)
                  .append("/")
                  .append(model.name)
                  .append("?wsdl\">");
              sb.append(model.name).append("</a></li>");
            }
          }
          sb.append("</ul></p></body></html>");

          writer.write(sb.toString());
          writer.flush();
          return null;
        } catch (Exception e) {
          sendError(response, "Unable to obtain WSDL", null);
          throw new EventHandlerException("Unable to obtain WSDL");
        }
      }
    }

    // not a wsdl request; invoke the service
    response.setContentType("text/xml");

    // request envelope
    SOAPEnvelope reqEnv = null;

    // get the service name and parameters
    try {
      XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(request.getInputStream());
      StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(xmlReader);
      reqEnv = (SOAPEnvelope) builder.getDocumentElement();

      // log the request message
      if (Debug.verboseOn()) {
        try {
          Debug.logInfo("Request Message:\n" + reqEnv + "\n", module);
        } catch (Throwable t) {
        }
      }
    } catch (Exception e) {
      sendError(response, "Problem processing the service", null);
      throw new EventHandlerException("Cannot get the envelope", e);
    }

    Debug.logVerbose("[Processing]: SOAP Event", module);

    String serviceName = null;
    try {
      SOAPBody reqBody = reqEnv.getBody();
      validateSOAPBody(reqBody);
      OMElement serviceElement = reqBody.getFirstElement();
      serviceName = serviceElement.getLocalName();
      Map<String, Object> parameters =
          UtilGenerics.cast(SoapSerializer.deserialize(serviceElement.toString(), delegator));
      try {
        // verify the service is exported for remote execution and invoke it
        ModelService model = dispatcher.getDispatchContext().getModelService(serviceName);

        if (model == null) {
          sendError(response, "Problem processing the service", serviceName);
          Debug.logError("Could not find Service [" + serviceName + "].", module);
          return null;
        }

        if (!model.export) {
          sendError(response, "Problem processing the service", serviceName);
          Debug.logError(
              "Trying to call Service [" + serviceName + "] that is not exported.", module);
          return null;
        }

        Map<String, Object> serviceResults = dispatcher.runSync(serviceName, parameters);
        Debug.logVerbose("[EventHandler] : Service invoked", module);

        createAndSendSOAPResponse(serviceResults, serviceName, response);

      } catch (GenericServiceException e) {
        if (UtilProperties.getPropertyAsBoolean("service", "secureSoapAnswer", true)) {
          sendError(
              response, "Problem processing the service, check your parameters.", serviceName);
        } else {
          if (e.getMessageList() == null) {
            sendError(response, e.getMessage(), serviceName);
          } else {
            sendError(response, e.getMessageList(), serviceName);
          }
          Debug.logError(e, module);
          return null;
        }
      }
    } catch (Exception e) {
      sendError(response, e.getMessage(), serviceName);
      Debug.logError(e, module);
      return null;
    }

    return null;
  }