/**
   * Writes an output of a GetView request to the <code>httpResponse</code> using the <code>mime
   * </code> type.
   *
   * @param output the image to be sent back
   * @param httpResponse the response to pipe the image
   * @param mime the type of image
   */
  private void writeImage(Image output, HttpServletResponse httpResponse, String mime) {
    try {

      OutputStream os = httpResponse.getOutputStream();
      httpResponse.setContentType(mime);

      if (mime.equalsIgnoreCase("image/jpg") || mime.equalsIgnoreCase("image/jpeg")) {

        OGCWebService service = WPVServiceFactory.createInstance();
        WPVSConfiguration config = (WPVSConfiguration) ((WPVService) service).getCapabilities();
        float quality = config.getDeegreeParams().getViewQuality();
        Encoders.encodeJpeg(os, (BufferedImage) output, quality);

        // TODO png accepted?
      } else if (mime.equalsIgnoreCase("image/png")) {
        Encoders.encodePng(os, (BufferedImage) output);
      } else if (mime.equalsIgnoreCase("image/tif") || mime.equalsIgnoreCase("image/tiff")) {
        Encoders.encodeTiff(os, (BufferedImage) output);
      } else if (mime.equalsIgnoreCase("image/bmp")) {
        Encoders.encodeBmp(os, (BufferedImage) output);
      } else {
        httpResponse.setContentType("text/xml");
        os = httpResponse.getOutputStream();
        OGCWebServiceException exce =
            new OGCWebServiceException("WMS:writeImage", "unsupported image format: " + mime);
        os.write(((Marshallable) exce).exportAsXML().getBytes());
      }

      os.close();
    } catch (Exception e) {
      LOG.logError("-", e);
    }
  }
  /**
   * Performs the passed OGCWebServiceRequest by accessing service from the pool and passing the
   * request to it
   *
   * @param request the incoming web service request
   * @param httpResponse the outgoing web serivce response
   * @throws ServiceException
   * @throws OGCWebServiceException
   */
  public void perform(OGCWebServiceRequest request, HttpServletResponse httpResponse)
      throws ServiceException, OGCWebServiceException {

    LOG.entering();

    LOG.logDebug(StringTools.concat(200, "Performing request: ", request.toString()));

    OGCWebService service = WPVServiceFactory.createInstance();

    try {
      Object response = service.doService(request);
      if (response instanceof WPVSCapabilities) {
        sendGetCapabilitiesResponse(httpResponse, (WPVSCapabilities) response);
      } else if (response instanceof GetViewResponse) {
        sendGetViewResponse(httpResponse, (GetViewResponse) response);
      } else {
        String s = (response == null ? "null response object" : response.getClass().getName());
        // this is not really nice...because excepts get cought later on below
        throw new OGCWebServiceException(
            getClass().getName(), StringTools.concat(200, "Unknown response class: '", s, "'."));
      }
    } catch (OGCWebServiceException e) {

      LOG.logError("Error performing WPVFS request.", e);
      if (request instanceof GetView
          && ((GetView) request).getExceptionFormat().equals("INIMAGE")) {
        sendExceptionImage(httpResponse, e, (GetView) request);

      } else {
        sendException(httpResponse, e);
      }
    }

    LOG.exiting();
  }