/**
   * appends the selected layers of a WMS to the passed <code>ViewContext</code>
   *
   * @param context
   * @throws ContextException
   * @throws MalformedURLException
   * @throws PortalException
   * @throws InvalidCapabilitiesException
   */
  private void appendWMS(RPCWebEvent rpc, ViewContext context)
      throws MalformedURLException, ContextException, PortalException,
          InvalidCapabilitiesException {

    RPCStruct struct = (RPCStruct) rpc.getRPCMethodCall().getParameters()[0].getValue();
    URL url = new URL((String) struct.getMember("WMSURL").getValue());
    String name = (String) struct.getMember("WMSNAME").getValue();
    String version = (String) struct.getMember("WMSVERSION").getValue();
    String layers = (String) struct.getMember("LAYERS").getValue();
    String formatName = (String) struct.getMember("FORMAT").getValue();
    boolean useAuthentification = false;
    if (struct.getMember("useAuthentification") != null) {
      String tmp = (String) struct.getMember("useAuthentification").getValue();
      useAuthentification = "true".equalsIgnoreCase(tmp);
    }

    List<String> list = StringTools.toList(layers, ";", true);

    WMSCapabilitiesDocument capa = null;
    try {
      StringBuffer sb = new StringBuffer(500);
      if ("1.0.0".equals(version)) {
        sb.append(url.toExternalForm()).append("?request=capabilities&service=WMS");
      } else {
        sb.append(url.toExternalForm()).append("?request=GetCapabilities&service=WMS");
      }
      if (useAuthentification) {
        HttpSession session = ((HttpServletRequest) getRequest()).getSession();
        String user =
            ((org.apache.jetspeed.om.security.BaseJetspeedUser)
                    session.getAttribute("turbine.user"))
                .getUserName();
        String password =
            ((org.apache.jetspeed.om.security.BaseJetspeedUser)
                    session.getAttribute("turbine.user"))
                .getPassword();
        if (!"anon".equals(user)) {
          sb.append("&user="******"&password="******"GetCapabilites for added WMS", sb.toString());
      capa = WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument(new URL(sb.toString()));
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      String msg = null;
      if ("1.0.0".equals(version)) {
        msg =
            StringTools.concat(
                500,
                "could not load WMS capabilities from: ",
                new URL(url.toExternalForm() + "?request=capabilities&service=WMS"),
                "; reason: ",
                e.getMessage());
      } else {
        msg =
            StringTools.concat(
                500,
                "could not load WMS capabilities from: ",
                new URL(url.toExternalForm() + "?request=GetCapabilities&service=WMS"),
                "; reason: ",
                e.getMessage());
      }
      throw new PortalException(msg);
    }
    WMSCapabilities capabilities = (WMSCapabilities) capa.parseCapabilities();
    String rootTitle = capabilities.getLayer().getTitle();

    // ----------------------------------------------------------------------------
    // stuff required by layerlist tree view
    Node root = context.getGeneral().getExtension().getLayerTreeRoot();
    // check if Node width this title already exists
    Node[] nodes = root.getNodes();
    int newNodeId = -1;
    for (int j = 0; j < nodes.length; j++) {
      if (nodes[j].getTitle().equals(rootTitle)) {
        newNodeId = nodes[j].getId();
        break;
      }
    }
    if (newNodeId == -1) {
      newNodeId = root.getMaxNodeId() + 1;
      Node newNode = new Node(newNodeId, root, rootTitle, true, false);
      Node[] newNodes = new Node[nodes.length + 1];
      newNodes[0] = newNode;
      for (int j = 0; j < nodes.length; j++) {
        newNodes[j + 1] = nodes[j];
      }

      root.setNodes(newNodes);
    }
    // ----------------------------------------------------------------------------
    for (int i = 0; i < list.size(); i++) {
      String[] lay = StringTools.toArray(list.get(i), "|", false);
      Server server = new Server(name, version, "OGC:WMS", url, capabilities);
      String srs = context.getGeneral().getBoundingBox()[0].getCoordinateSystem().getPrefixedName();
      Format format = new Format(formatName, true);
      FormatList fl = new FormatList(new Format[] {format});
      // read available styles from WMS capabilities and add them
      // to the WMC layer
      org.deegree.ogcwebservices.wms.capabilities.Layer wmslay = capabilities.getLayer(lay[0]);
      org.deegree.ogcwebservices.wms.capabilities.Style[] wmsstyles = wmslay.getStyles();
      Style[] styles = null;
      if (wmsstyles == null || wmsstyles.length == 0) {
        // a wms capabilities layer may offeres one or more styles for
        // a layer but it don't have to. But WMC must have at least one
        // style for each layer; So we set a default style in the case
        // a wms does not declares one
        styles = new Style[1];
        styles[0] = new Style("", "default", "", null, true);
      } else {
        styles = new Style[wmsstyles.length];
        for (int j = 0; j < styles.length; j++) {
          boolean isDefault =
              wmsstyles[j].getName().toLowerCase().indexOf("default") > -1
                  || wmsstyles[j].getName().trim().length() == 0;
          ImageURL legendURL = null;
          LegendURL[] lUrl = wmsstyles[j].getLegendURL();
          if (lUrl != null && lUrl.length > 0) {
            legendURL =
                new ImageURL(
                    lUrl[0].getWidth(),
                    lUrl[0].getHeight(),
                    lUrl[0].getFormat(),
                    lUrl[0].getOnlineResource());
          }
          styles[j] =
              new Style(
                  wmsstyles[j].getName(),
                  wmsstyles[j].getTitle(),
                  wmsstyles[j].getAbstract(),
                  legendURL,
                  isDefault);
        }
      }

      StyleList styleList = new StyleList(styles);
      BaseURL mdUrl = null;
      MetadataURL[] mdUrls = wmslay.getMetadataURL();
      if (mdUrls != null && mdUrls.length == 1 && mdUrls[0] != null) {
        mdUrl = mdUrls[0];
      }

      int authentication = LayerExtension.NONE;
      if (useAuthentification) {
        authentication = LayerExtension.USERPASSWORD;
      }
      LayerExtension lex =
          new LayerExtension(
              null,
              false,
              wmslay.getScaleHint().getMin(),
              wmslay.getScaleHint().getMax(),
              false,
              authentication,
              newNodeId,
              false,
              null);
      Layer newLay =
          new Layer(
              server,
              lay[0],
              lay[1],
              null,
              new String[] {srs},
              null,
              mdUrl,
              fl,
              styleList,
              wmslay.isQueryable(),
              false,
              lex);
      if (context
              .getLayerList()
              .getLayer(newLay.getName(), server.getOnlineResource().toExternalForm())
          == null) {
        context.getLayerList().addLayerToTop(newLay);
      }
    }
    try {
      XMLFragment xml = XMLFactory.export(context);
      System.out.println(xml.getAsPrettyString());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 /**
  * Layers may include a &lt;ScaleHint&gt; element that suggests minimum and maximum scales for
  * which it is appropriate to display this layer. Because WMS output is destined for output
  * devices of arbitrary size and resolution, the usual definition of scale as the ratio of map
  * size to real-world size is not appropriate here. The following definition of Scale Hint is
  * recommended. Consider a hypothetical map with a given Bounding Box, width and height. The
  * central pixel of that map (or the pixel just to the northwest of center) will have some size,
  * which can be expressed as the ground distance in meters of the southwest to northeast diagonal
  * of that pixel. The two values in ScaleHint are the minimum and maximum recommended values of
  * that diagonal. It is recognized that this definition is not geodetically precise, but at the
  * same time the hope is that by including it conventions will develop that can be later specified
  * more clearly.
  *
  * @return the scale hint
  */
 public ScaleHint getScaleHint() {
   if ((parent != null) && (scaleHint == null)) {
     return parent.getScaleHint();
   }
   return scaleHint;
 }