// implementacja kontrolera aplikacji w celu obsugi zapyta usug web
class WebServiceApplicationController implements ApplicationController {
 
  HandlerChain handlerChain;
 
  public WebServiceApplicationController() { }
 
  public void init() {
    handlerChain = new HandlerChain();
    handlerChain.addHandler(new AuditHAndler());
    handlerChain.addHAndler(new AuthenticationHandler());
  }
 
  public ResponseContext handleRequest(RequestContext requestContext) {
    ResponseContext = responseContext = null;
    try {
      SOAPRequestContext soapRequestContext = (SOAPRequestContext)requestContext;
 
      // utworzenie komunikatu SOAP ze strumienia HTTP
      SOAPMessage soapMessage = soapRequestContext.getSOAPMessage();
 
      // przetworzenie wstpne komunikatu przez obsug komunikatw
      handlerChain.handleRequest(soapMessage);
 
      // uzyskanie nazwy polecenia z zapytania specyficznego dla protokou
      String commandName = soapRequestContext.getCommandName();
 
      // zamiana nazwy polecenia na obiekt polecenia
      CommandFactory commandFactory = CommandFactory.getInstance();
      Command command = commandFactory.getCommand(commandName);
 
      // wywoanie polecenie przy uyciu CommandProcessor
      CommandProcessor commandProcessor = new CommandProcessor();
      Object result = commandProcessor.invoke(command, requestContext);
 
      // utworzenie kontekstu odpowiedzi na podstawie wynikw polecenia
      responseContext = 
        ResponseContextFactory.getInstance().createResponseContext(result, null);
    } catch(java.lang.InstantiationException e) {
      // obsuga wyjtku
    } catch(java.lang.IllegalAccessException e) {
      // obsuga wyjtku
    }
    return responseContext;
  }
 
  public void handleResponse(RequestContext requestContext,
                             ResponseContext responseContext) {
    try {
      // utworzenie odpowiedzi SOAP z otrzymanych wynikw obiektu polecenia
      SOAPMessage soapMessage = null;
      Object payload = responseContext.getData();
 
      soapMessage = SOAPHttpFacade.createSOAPMessage(payload);
      if (soapMessage != null) {
        // przetwarzanie kocowe komunikatu przez obsug komunikatw
        handlerChain.handleResponse(soapMessage);
 
        // musimy uy saveChanges, gdy zamierzamy uy MimeHeaders do ustawienia
        // informacji o odpowiedzi HTTP. MimeHeaders s generowane jako cz zapisu
 
        if (soapMessage.saveRequired()) {
          soapMessage.saveChanges();
        }
 
        HttpServletResponse response = 
          (HttpServletResponse)responseContext.getResponse();
        responseContext.getResponse();
        response.setStatus(HttpServletResponse.SC_OK);
 
        SOAPHttpFacade.putHeaders(soapMessage.getMimeHeaders(), response);
        writeResponse((HttpServletResponse)responseContext.getResponse(),
                      soapMessage);
      }
    } catch (SOAPException e) {
      // obsuga wyjtku
    }
  }

  public void destroy() { }
 
  private void writeResponse(HttpServletResponse response,
                             SOAPMessage soapResponse) {
    try {
      // zapisanie komunikatu w strumieniu odpowiedzi
      response.setContentType("text xml");  // komunikat SOAP 1.1
      OutputStream outputStream = response.getOutputStream();
      soapResponse.writeTo(outputStream);
      outputStream.flush();
    } catch(SOAPException e) {
      // obsuga wyjtku
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } catch(java.io.IOException e) {
      // obsuga wyjtku
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
  }
  ...
}
