public String getWMSLegendUrl(String urlServerWMS, String layerId, String stylesName) {
   String legendUrl = "";
   org.gvsig.remoteclient.wms.WMSStyle layerStyle = null;
   org.gvsig.remoteclient.wms.WMSStyle layerStyleDefault = null;
   // Create conexion with server WMS
   try {
     WMSClient wms = new WMSClient(urlServerWMS);
     wms.connect(null);
     WMSLayer layer = wms.getLayer(layerId);
     if (layer != null) {
       List<org.gvsig.remoteclient.wms.WMSStyle> lstStyles = layer.getStyles();
       if (StringUtils.isNotEmpty(stylesName)) {
         List<String> styList = Arrays.asList(stylesName.split(","));
         for (org.gvsig.remoteclient.wms.WMSStyle wmsStyle : lstStyles) {
           // default not
           if (styList.contains(wmsStyle.getName())
               && wmsStyle.getName().toLowerCase() != "default") {
             layerStyle = wmsStyle;
             break;
           } else {
             if (wmsStyle.getName().toLowerCase() == "default") {
               layerStyleDefault = wmsStyle;
             }
           }
         }
         // if layer style isn't defined on parameter, set default style
         // or the first
         if (layerStyle == null) {
           if (layerStyleDefault == null && !lstStyles.isEmpty()) {
             layerStyle = lstStyles.get(0);
           } else {
             layerStyle = layerStyleDefault;
           }
         }
       } else {
         // if haven't styles, get the first
         if (!lstStyles.isEmpty()) {
           layerStyle = lstStyles.get(0);
         }
       }
       if (layerStyle != null) {
         legendUrl = layerStyle.getLegendURLOnlineResourceHRef();
       }
     }
   } catch (Exception exc) {
     // Show exception in log
     logger.error("Exception on getWMSLegendUrl", exc);
   }
   return legendUrl;
 }
  /** {@inheritDoc}} */
  @Override
  public String getFeatureInfoFromWMS(
      String urlServer,
      String crs,
      Vector<String> layers,
      Vector<String> styles,
      int x,
      int y,
      int height,
      int width,
      List<String> bounds) {
    String featureInfo = null;

    Float xmin = new Float(Float.parseFloat(bounds.get(0)));
    Float ymin = new Float(Float.parseFloat(bounds.get(1)));
    Float xmax = new Float(Float.parseFloat(bounds.get(2)));
    Float ymax = new Float(Float.parseFloat(bounds.get(3)));

    Rectangle2D.Float extent = new Rectangle2D.Float();
    extent.setFrameFromDiagonal(xmin, ymin, xmax, ymax);

    WMSStatus status = new WMSStatus();
    status.setSrs(crs);
    status.setStyles(styles);
    status.setLayerNames(layers);
    status.setOnlineResource(urlServer);
    status.setExtent(extent);
    status.setHeight(height);
    status.setWidth(width);
    status.setInfoFormat("text/html");
    status.setExceptionFormat("text/plain");

    try {
      WMSClient wms = new WMSClient(urlServer);
      wms.connect(null);

      featureInfo = wms.getFeatureInfo(status, x, y, 0, null);

    } catch (Exception exc) {
      // Show exception in log
      logger.error("Exception on getFeatureInfoFromWMS", exc);
      throw new ServerGeoException();
    }
    return featureInfo;
  }
  public ServiceMetadata getMetadataInfoFromWMS(String urlServer) {

    ServiceMetadata servMetadata = new ServiceMetadata();

    try {
      WMSClient wms = new WMSClient(urlServer);
      wms.connect(null);

      // set server information
      WMSServiceInformation serviceInfo = wms.getServiceInformation();

      servMetadata.setAbstractStr(serviceInfo.abstr);
      servMetadata.setFees(serviceInfo.fees);
      servMetadata.setKeywords(serviceInfo.keywords);
      servMetadata.setName(serviceInfo.name);
      servMetadata.setTitle(serviceInfo.title);
      servMetadata.setUrl(urlServer);
      servMetadata.setVersion(serviceInfo.version);
      // I can't get from service the accessConstraints value
      // servMetadata.setAccessConstraints(accessConstraints);

      // get layers
      TreeMap layers = wms.getLayers();
      if (layers != null) {
        List<Layer> lstLayers = new ArrayList<Layer>();
        Set<String> keys = layers.keySet();
        for (String key : keys) {
          WMSLayer wmsLayer = (WMSLayer) layers.get(key);
          Layer layer = new Layer();
          layer.setName(wmsLayer.getName());
          layer.setTitle(wmsLayer.getTitle());
          lstLayers.add(layer);
        }
        servMetadata.setLayers(lstLayers);
      }

      // get operations
      Hashtable supportedOperations = serviceInfo.getSupportedOperationsByName();
      if (supportedOperations != null) {
        List<OperationsMetadata> lstOperations = new ArrayList<OperationsMetadata>();
        Set<String> keys = supportedOperations.keySet();
        for (String key : keys) {
          OperationsMetadata opMetadata = new OperationsMetadata();
          opMetadata.setName(key);
          opMetadata.setUrl((String) supportedOperations.get(key));
          lstOperations.add(opMetadata);
        }
        servMetadata.setOperations(lstOperations);
      }
      // get contact address
      ContactAddressMetadata contactAddress = null;
      if (StringUtils.isNotEmpty(serviceInfo.address)
          || StringUtils.isNotEmpty(serviceInfo.addresstype)
          || StringUtils.isNotEmpty(serviceInfo.place)
          || StringUtils.isNotEmpty(serviceInfo.country)
          || StringUtils.isNotEmpty(serviceInfo.postcode)
          || StringUtils.isNotEmpty(serviceInfo.province)) {
        contactAddress = new ContactAddressMetadata();
        contactAddress.setAddress(serviceInfo.address);
        contactAddress.setAddressType(serviceInfo.addresstype);
        contactAddress.setCity(serviceInfo.place);
        contactAddress.setCountry(serviceInfo.country);
        contactAddress.setPostCode(serviceInfo.postcode);
        contactAddress.setStateProvince(serviceInfo.province);
      }

      // get contact info
      ContactMetadata contactMetadata = null;
      if (contactAddress != null
          || StringUtils.isNotEmpty(serviceInfo.email)
          || StringUtils.isNotEmpty(serviceInfo.fax)
          || StringUtils.isNotEmpty(serviceInfo.organization)
          || StringUtils.isNotEmpty(serviceInfo.personname)
          || StringUtils.isNotEmpty(serviceInfo.function)
          || StringUtils.isNotEmpty(serviceInfo.phone)) {
        contactMetadata = new ContactMetadata();
        contactMetadata.setContactAddress(contactAddress);
        contactMetadata.setEmail(serviceInfo.email);
        contactMetadata.setFax(serviceInfo.fax);
        contactMetadata.setOrganization(serviceInfo.organization);
        contactMetadata.setPerson(serviceInfo.personname);
        contactMetadata.setPosition(serviceInfo.function);
        contactMetadata.setTelephone(serviceInfo.phone);
      }
      servMetadata.setContact(contactMetadata);

    } catch (Exception exc) {
      // Show exception in log
      logger.error("Exception on getMetadataInfoFromWMS", exc);
      throw new ServerGeoException();
    }

    return servMetadata;
  }
 /**
  * Get the four coordinates that represent the bounding box which includes all the layers
  * indicated for a WMS Server
  *
  * @param urlServer Url of the WMS server to connect and get the data
  * @param crs CRS of the bounding box
  * @param layers List of layers to include in the bounding box calculated
  * @return A list of coordinates that represents the bounding box calculated (minX, minY, maxX,
  *     maxY). null if haven't valid bounding box
  */
 private List<String> getWMSLayersBoundingBox(
     String urlServer, String crs, TreeSet<String> layers) {
   List<String> boundingBox = new ArrayList<String>();
   double xMax = 0;
   double xMin = 0;
   double yMin = 0;
   double yMax = 0;
   BoundaryBox bbox = null;
   try {
     WMSClient wms = new WMSClient(urlServer);
     wms.connect(null);
     if (layers != null) {
       Iterator<String> iterLayers = layers.iterator();
       // get the first element
       WMSLayer layer = wms.getLayer(iterLayers.next());
       if (layer != null) {
         bbox = layer.getBbox(crs);
         if (bbox != null) {
           xMax = bbox.getXmax();
           yMax = bbox.getYmax();
           xMin = bbox.getXmin();
           yMin = bbox.getYmin();
         }
       }
       while (iterLayers.hasNext()) {
         layer = wms.getLayer(iterLayers.next());
         bbox = layer.getBbox(crs);
         if (bbox != null) {
           // if getXmax is greater than xMax
           if (Double.compare(xMax, bbox.getXmax()) < 0) {
             xMax = bbox.getXmax();
           }
           // if getYmax is greater than yMax
           if (Double.compare(yMax, bbox.getYmax()) < 0) {
             yMax = bbox.getYmax();
           }
           // if getXmin is less than xMin
           if (Double.compare(xMin, bbox.getXmin()) > 0) {
             xMin = bbox.getXmin();
           }
           // if getYmin is less than yMin
           if (Double.compare(yMin, bbox.getYmin()) > 0) {
             yMin = bbox.getYmin();
           }
         }
       }
       if (bbox != null) {
         boundingBox.add(String.valueOf(xMin));
         boundingBox.add(String.valueOf(yMin));
         boundingBox.add(String.valueOf(xMax));
         boundingBox.add(String.valueOf(yMax));
       }
     }
   } catch (Exception exc) {
     // Show exception in log and create ServerGeoException which is
     // captured on controller and puts message to ajax response
     logger.error("Exception on getWMSLayersBoundingBox", exc);
     throw new ServerGeoException();
   }
   return boundingBox;
 }
  /*
   * (non-Javadoc)
   * @see es.gva.dgti.gvgeoportal.service.ogc.OGCInfoService
   * #getCapabilitiesFromWMS(String, String, String, boolean)
   */
  public WMSInfo getCapabilitiesFromWMS(
      String urlServerWMS, TreeSet<String> listCrs, String format, boolean isCalledByWizard)
      throws ServerGeoException {
    WMSInfo wmsInfo = new WMSInfo();
    // put url on object WMSInfo
    wmsInfo.setServiceUrl(urlServerWMS);

    // Create hashmap to add the layers getted to the WMSInfo object
    Map<String, org.gvsig.framework.web.ogc.WMSLayer> layersMap =
        new HashMap<String, org.gvsig.framework.web.ogc.WMSLayer>();

    // Create conexion with server WMS
    try {
      WMSClient wms = new WMSClient(urlServerWMS);
      wms.connect(null);

      // set server information
      WMSServiceInformation serviceInfo = wms.getServiceInformation();
      wmsInfo.setServiceAbstract(serviceInfo.abstr);
      wmsInfo.setServiceName(serviceInfo.name);
      wmsInfo.setServiceTitle(serviceInfo.title);

      // set id of the request wmsinfo (service name + calendar)
      int hashCode = (serviceInfo.name + Calendar.getInstance()).hashCode();
      wmsInfo.setId(hashCode);

      // get and set version
      String version = wms.getVersion();
      wmsInfo.setVersion(version);

      // get and set formats
      Vector formatVector = wms.getFormats();
      TreeSet<String> formatSet = new TreeSet<String>();
      formatSet.addAll(formatVector);
      wmsInfo.setFormatsSupported(formatSet);
      if (StringUtils.isEmpty(format)) {
        format = getFirstFormatSupported(formatSet);
        wmsInfo.setFormatSelected(format);
      }
      // check format
      if (isCalledByWizard || (!isCalledByWizard && formatVector.contains(format))) {
        wmsInfo.setFormatSelected(format);
        // get root layer
        WMSLayer rootLayer = wms.getRootLayer();
        // get crs (srs) (belong to layer)
        Vector crsVector = rootLayer.getAllSrs();
        // get and set all common crs supported
        TreeSet<String> crsTreeSet = new TreeSet<String>();
        crsTreeSet.addAll(crsVector);
        wmsInfo.setCrsSupported(crsTreeSet);

        // Create tree with layer values
        List<TreeNode> tree = new ArrayList<TreeNode>();

        // Create root node
        ArrayList<WMSLayer> children = rootLayer.getChildren();
        TreeNode rootNode = new TreeNode("rootLayer_" + rootLayer.getName());
        rootNode.setTitle(rootLayer.getTitle());
        if (children.isEmpty()) {
          rootNode.setFolder(false);
        } else {
          rootNode.setFolder(true);
          rootNode.setExpanded(true);
          generateWMSChildrenNodes(children, tree, listCrs, rootNode, layersMap, wmsInfo);
        }

        // Set childrenLayers paramrootLayer.getChildren()
        wmsInfo.setChildrenCount(children.size());

        // Only register the tree if it has a layer with crs defined
        if (rootNode.hasChildren()) {
          tree.add(rootNode);
          wmsInfo.setLayersTree(tree);
        }

        TreeSet<String> selCrs = new TreeSet<String>();
        if (listCrs.isEmpty()) {
          selCrs.addAll(wmsInfo.getCrsSupported());
        } else {
          selCrs.addAll(CollectionUtils.intersection(listCrs, wmsInfo.getCrsSupported()));
        }
        wmsInfo.setCrsSelected(selCrs);

        // Add map that contains info of all layers
        wmsInfo.setLayers(layersMap);
      }
    } catch (Exception exc) {
      // Show exception in log and create ServerGeoException which is
      // captured on controller and puts message to ajax response
      logger.error("Exception on getCapabilitiesFromWMS", exc);
      throw new ServerGeoException();
    }

    return wmsInfo;
  }