/**
   * loads a context defined by its absolut path passed in URL encoded parameter 'FILENAME'
   *
   * @throws PortalException
   */
  void doLoadwmc(User user) throws PortalException {

    String filename = null;
    try {
      filename = URLDecoder.decode(parameter.get("FILENAME"), CharsetUtils.getSystemCharset());
    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
    }
    File file = new File(filename);
    String cntxid = file.getName();

    PortletSet ps = portlet.getPortletConfig().getPortletSet();
    String mapid = ps.getPortletByName("iGeoPortal:MapActionPortlet").getID();
    Portlet port = ps.getPortletByID(mapid);
    port.getPortletConfig().setInitParameter(INIT_WMC, cntxid);

    String mwinid = getInitParam(INIT_MAPPORTLETID);
    port = ps.getPortletByID(mwinid);
    port.getPortletConfig().setInitParameter(INIT_WMC, cntxid);

    request.setAttribute(PARAM_MAPPORTLET, mwinid);

    ViewContext vc = null;
    try {
      vc = WebMapContextFactory.createViewContext(file.toURI().toURL(), user, null);
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      return;
    }

    setCurrentMapContext(vc, getInitParam(INIT_MAPPORTLETID));
    setCurrentMapContextName(getInitParam(INIT_MAPPORTLETID), cntxid);
  }
  // TODO common to WMS
  private void writeImage(Object output, String mime, HttpServletResponse resp) {
    try {
      OutputStream os = null;
      resp.setContentType(mime);

      if (mime.equalsIgnoreCase("image/gif")) {
        os = resp.getOutputStream();
        ImageUtils.saveImage((BufferedImage) output, os, "gif", 1);
      } else if (mime.equalsIgnoreCase("image/jpg") || mime.equalsIgnoreCase("image/jpeg")) {
        os = resp.getOutputStream();
        ImageUtils.saveImage((BufferedImage) output, os, "jpeg", 1);
      } else if (mime.equalsIgnoreCase("image/png")) {
        os = resp.getOutputStream();
        ImageUtils.saveImage((BufferedImage) output, os, "png", 1);
      } else if (mime.equalsIgnoreCase("image/tif") || mime.equalsIgnoreCase("image/tiff")) {
        os = resp.getOutputStream();
        ImageUtils.saveImage((BufferedImage) output, os, "tif", 1);
      } else if (mime.equalsIgnoreCase("image/bmp")) {
        os = resp.getOutputStream();
        ImageUtils.saveImage((BufferedImage) output, os, "bmp", 1);
      } else if (mime.equalsIgnoreCase("image/svg+xml")) {
        os = resp.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        DOMPrinter.printNode(pw, (Node) output);
        pw.close();
      } else {
        resp.setContentType("text/xml; charset=" + CharsetUtils.getSystemCharset());
        os = resp.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.getMessage(), e);
    }
  }
  /** @see javax.servlet.GenericServlet#init() */
  @Override
  public void init() throws ServletException {

    synchronized (OGCServletController.class) {
      if (LOG == null) {
        // hack to figure out and set the context path name
        // for a laugh, see http://marc.info/?l=tomcat-user&m=109215904113904&w=2 and the related
        // thread
        // http://marc.info/?t=109215871400004&r=1&w=2
        String path = getServletContext().getRealPath("");
        String[] ps = path.split("[/\\\\]");
        path = ps[ps.length - 1];
        // heuristics are always a charm (and work best for tomcat in this case)
        if (isDigit(path.charAt(0)) && path.indexOf("-") != -1) {
          path = path.split("-", 2)[1];
        }
        // note that setting this changes it on a JVM GLOBAL BASIS, so it WILL GET OVERWRITTEN in
        // subsequent
        // deegree startups! (However, since the log4j.properties will only be read on startup, this
        // hack is
        // useful anyway)
        setProperty("context.name", path);

        LOG = getLogger(OGCServletController.class);
      }
    }

    super.init();
    LOG.logDebug("Logger for " + this.getClass().getName() + " initialized.");

    SERVICE_FACTORIES_MAPPINGS.put(CSWHandler.class, "org.deegree.ogcwebservices.csw.CSWFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WFSHandler.class, "org.deegree.ogcwebservices.wfs.WFServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WCSHandler.class, "org.deegree.ogcwebservices.wcs.WCServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WMSHandler.class, "org.deegree.ogcwebservices.wms.WMServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WPVSHandler.class, "org.deegree.ogcwebservices.wpvs.WPVServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WMPSHandler.class, "org.deegree.ogcwebservices.wmps.WMPServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WPSHandler.class, "org.deegree.ogcwebservices.wps.WPServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WASSHandler.class, "org.deegree.ogcwebservices.wass.common.WASServiceFactory");
    SERVICE_FACTORIES_MAPPINGS.put(
        WCTSHandler.class, "org.deegree.ogcwebservices.wcts.WCTServiceFactory");

    LOG.logInfo("-------------------------------------------------------------------------------");
    LOG.logInfo("Starting deegree version " + Version.getVersion());
    LOG.logInfo("- context        : " + this.getServletContext().getServletContextName());
    LOG.logInfo("- real path      : " + this.getServletContext().getRealPath("/"));
    LOG.logInfo("- java version   : " + System.getProperty("java.version") + "");
    LOG.logInfo(
        "- dom builder    : " + DocumentBuilderFactory.newInstance().getClass().getName() + "");
    LOG.logInfo("- xslt builder   : " + TransformerFactory.newInstance().getClass().getName() + "");
    LOG.logInfo("- system charset : " + CharsetUtils.getSystemCharset());
    LOG.logInfo("- default charset: " + Charset.defaultCharset());
    LOG.logInfo("- server info    : " + this.getServletContext().getServerInfo());
    logIfThere("proxyHost");
    logIfThere("proxyPort");
    logIfThere("noProxyHosts");
    logIfThere("nonProxyHosts");
    logIfThere("http.proxyHost");
    logIfThere("http.proxyPort");
    logIfThere("http.noProxyHosts");
    logIfThere("http.nonProxyHosts");
    logIfThere("ftp.proxyHost");
    logIfThere("ftp.proxyPort");
    logIfThere("ftp.noProxyHosts");
    logIfThere("ftp.nonProxyHosts");
    logIfThere("https.proxyHost");
    logIfThere("https.proxyPort");
    logIfThere("https.noProxyHosts");
    logIfThere("https.nonProxyHosts");
    try {
      LOG.logInfo("- ip             : " + InetAddress.getLocalHost().getHostAddress());
      LOG.logInfo("- host name      : " + InetAddress.getLocalHost().getHostName());
      LOG.logInfo("- domain name    : " + InetAddress.getLocalHost().getCanonicalHostName());
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
    }
    LOG.logInfo("-------------------------------------------------------------------------------");
    this.initServices(getServletContext());
    checkServerCompatibility();
    LOG.logInfo("-------------------------------------------------------------------------------");
    String tmpServiceList = this.getServiceList();
    if (tmpServiceList != null && !("".equals(tmpServiceList.trim()))) {
      LOG.logInfo(
          "Initialized successfully (context '"
              + this.getServletContext().getServletContextName()
              + "'):");
      String[] tmpServices = tmpServiceList.split(",");
      for (String service : tmpServices) {
        // Added a check for the alternative service name, because it should not be outputed twice
        // for the csw.
        if (!OGCRequestFactory.CSW_SERVICE_NAME_EBRIM.toUpperCase().equals(service)) {
          LOG.logInfo("- " + service);
        }
      }
    } else {
      LOG.logError(
          "An Error occured while initializing context '"
              + this.getServletContext().getServletContextName()
              + "', no services are available.");
    }

    LOG.logInfo("-------------------------------------------------------------------------------");
    // Sets the attributes for tomcat -> application.getAttribute(); in jsp sites
    this.getServletContext().setAttribute("deegree_ogc_services", this.getServiceList());
  }