private void initServices(ServletContext context) throws ServletException {

    // get list of OGC services
    String serviceList = this.getRequiredInitParameter(SERVICE);

    String[] serviceNames = StringTools.toArray(serviceList, ",", false);

    ServiceLookup lookup = ServiceLookup.getInstance();
    for (int i = 0; i < serviceNames.length; i++) {
      LOG.logInfo(
          StringTools.concat(100, "---- Initializing ", serviceNames[i].toUpperCase(), " ----"));
      try {
        String className = this.getRequiredInitParameter(serviceNames[i] + HANDLER_CLASS);
        Class<?> handlerClzz = Class.forName(className);

        // initialize each service factory
        String s = this.getRequiredInitParameter(serviceNames[i] + HANDLER_CONF);
        URL serviceConfigurationURL = WebappResourceResolver.resolveFileLocation(s, context, LOG);

        // set configuration
        LOG.logInfo(
            StringTools.concat(
                300,
                "Reading configuration for ",
                serviceNames[i].toUpperCase(),
                " from URL: '",
                serviceConfigurationURL,
                "'."));

        String factoryClassName = SERVICE_FACTORIES_MAPPINGS.get(handlerClzz);

        Class<?> factory = Class.forName(factoryClassName);
        Method method = factory.getMethod("setConfiguration", new Class[] {URL.class});
        method.invoke(factory, new Object[] {serviceConfigurationURL});

        // The csw-ebrim profile adds an alternative service name, it too is registred with the CSW
        // handler.
        if ("CSW".equals(serviceNames[i].toUpperCase())) {
          lookup.addService(OGCRequestFactory.CSW_SERVICE_NAME_EBRIM.toUpperCase(), handlerClzz);
        }
        // put handler to available service list
        lookup.addService(serviceNames[i].toUpperCase(), handlerClzz);

        LOG.logInfo(
            StringTools.concat(300, serviceNames[i].toUpperCase(), " successfully initialized."));
      } catch (ServletException e) {
        LOG.logError(e.getMessage(), e);
      } catch (InvocationTargetException e) {
        e.getTargetException().printStackTrace();
        LOG.logError(this.produceMessage(ERR_MSG, new Object[] {serviceNames[i]}), e);
      } catch (Exception e) {
        LOG.logError("Can't initialize OGC service:" + serviceNames[i], e);
      }
    }
  }
  /**
   * sets all request attributes required by a map from the passed ViewContext
   *
   * @param vc
   */
  private void setMapWindowAttributes() {

    String tmp = getInitParam(INIT_PANBUTTONS);
    ArrayList<String> pB = new ArrayList<String>(20);
    if (tmp != null) {
      String[] panButtons = StringTools.toArray(tmp, ",;", true);
      List<String> list = Arrays.asList(panButtons);
      pB.addAll(list);
    }

    setAttributes(pB);
  }
  /**
   * moves the layer passed through by the HTTP request down for one position
   *
   * @throws PortalException
   */
  void moveDown() throws PortalException {

    ViewContext vc = getCurrentViewContext(portlet.getID());
    String tmp = parameter.get(PARAM_LAYER);
    String[] s = StringTools.toArray(tmp, "|", false);
    LOG.logDebug(
        StringTools.concat(150, "moving layer: ", s[0], " map model: ", portlet.getID(), " down"));
    MapModelAccess mma = new DefaultMapModelAccess(vc);
    try {
      vc = mma.swapLayers(new QualifiedName(null, s[0], null), new URL(s[1]), "OGC:WMS", false);
    } catch (MalformedURLException e) {
      throw new PortalException("no valid URL", e);
    }
    setCurrentMapContext(vc, portlet.getID());
  }
Exemplo n.º 4
0
 /**
  * @param name
  * @param select
  * @throws UnknownCRSException
  */
 Table(String name, String select, List<Pair<String, String>> geometryColumns)
     throws UnknownCRSException {
   this.name = name;
   this.select = select.trim();
   String[] tmp = StringTools.toArray(select, " ", false);
   for (String value : tmp) {
     if (value.startsWith("$")) {
       variables.add(value);
     }
   }
   for (Pair<String, String> pair : geometryColumns) {
     CoordinateSystem crs = CRSFactory.create(pair.second);
     Pair<String, CoordinateSystem> p = new Pair<String, CoordinateSystem>(pair.first, crs);
     this.geometryColumns.add(p);
   }
 }
  /**
   * validates if the requested layer is valid against the policy/condition. If the passed user <>
   * null this is checked against the user- and rights-management system/repository
   *
   * @param condition
   * @param layer
   * @throws InvalidParameterValueException
   */
  private void validateLayer(Condition condition, String layer, String style)
      throws InvalidParameterValueException {

    OperationParameter op = condition.getOperationParameter(LAYER);

    // version is valid because no restrictions are made
    if (op.isAny()) {
      return;
    }

    List<String> v = op.getValues();

    // seperate layers from assigned styles
    Map<String, String> map = new HashMap<String, String>();
    for (int i = 0; i < v.size(); i++) {
      String[] tmp = StringTools.toArray(v.get(i), "|", false);
      map.put(tmp[0], tmp[1]);
    }

    String vs = map.get(layer);

    if (vs == null) {
      if (!op.isUserCoupled()) {
        throw new InvalidParameterValueException(INVALIDLAYER + layer);
      }
      userCoupled = true;
    } else if (!style.equalsIgnoreCase("default")
        && vs.indexOf("$any$") < 0
        && vs.indexOf(style) < 0) {
      if (!op.isUserCoupled()) {
        // a style is valid for a layer if it's the default style
        // or the layer accepts any style or a style is explicit defined
        // to be valid
        throw new InvalidParameterValueException(INVALIDSTYLE + layer + ':' + style);
      }
      userCoupled = true;
    }
  }
  /**
   * sets the name of the the layers that are activated for feature info requests in the uses WMC
   */
  void setCurrentFILayer() {
    String tmp = parameter.get(PARAM_CURRENTFILAYER);
    String[] fiLayer = StringTools.toArray(tmp, ",", false);
    if (fiLayer != null) {
      List<String> list = Arrays.asList(fiLayer);
      list = new ArrayList<String>(list);

      ViewContext vc = getCurrentViewContext(portlet.getID());
      LayerList layerList = vc.getLayerList();
      Layer[] layers = layerList.getLayers();
      for (int i = 0; i < layers.length; i++) {
        try {
          if (list.contains(layers[i].getName())) {
            layers[i].getExtension().setSelectedForQuery(true);
          } else {
            layers[i].getExtension().setSelectedForQuery(false);
          }
        } catch (Exception e) {
          // TODO: handle exception
          LOG.logError(e.getMessage(), e);
        }
      }
    }
  }
Exemplo n.º 7
0
  /**
   * 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();
    }
  }