/*
   * (non-Javadoc)
   *
   * @see org.deegree.security.AbstractAuthentication#authenticate(java.util.Map)
   */
  public User authenticate(Map<String, String> params) throws WrongCredentialsException {

    String tmp = initParams.get(INIT_PARAM_PATTERN);
    List<String> patterns = StringTools.toList(tmp, ",;", true);

    String ipAddress = params.get(AUTH_PARAM_IPADDRESS);
    if (ipAddress != null) {
      for (String p : patterns) {
        if (ipAddress.matches(p)) {
          User usr = null;
          try {
            SecurityAccessManager sam = SecurityAccessManager.getInstance();
            // use matching pattern as username and password
            usr = sam.getUserByName(p);
            usr.authenticate(null);
          } catch (Exception e) {
            LOG.logError(e.getMessage());
            String msg = Messages.getMessage("OWSPROXY_USER_AUTH_ERROR", ipAddress);
            throw new WrongCredentialsException(msg);
          }
          return usr;
        }
      }
      throw new WrongCredentialsException(
          Messages.getMessage("OWSPROXY_USER_AUTH_ERROR", ipAddress));
    }
    return null;
  }
  /*
   * (non-Javadoc)
   *
   * @see org.deegree.igeo.modules.remotecontrol.RequestHandler#perform(java.util.Map,
   * org.deegree.igeo.ApplicationContainer)
   */
  public String perform(Map<String, String> paramater, ApplicationContainer<?> appContainer)
      throws ModuleException {
    String tmp = paramater.get("OBJECTIDS");
    if (tmp == null || tmp.length() == 0) {
      throw new ModuleException(Messages.get("$DG10091"));
    }
    List<String> objIds = StringTools.toList(tmp, ",;", true);
    tmp = paramater.get("LAYER");
    if (tmp == null) {
      throw new ModuleException(Messages.get("$DG10092"));
    }

    MapModel mapModel = appContainer.getMapModel(null);
    LocalMapModelVisitor mv = new LocalMapModelVisitor(tmp);
    try {
      mapModel.walkLayerTree(mv);
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      throw new ModuleException(e.getMessage());
    }

    Layer layer = mv.getResult();
    if (layer == null) {
      throw new ModuleException(Messages.get("$DG10093", tmp));
    }
    layer.setVisible(true);

    // first select features in layer
    List<Identifier> ids = new ArrayList<Identifier>();
    for (String id : objIds) {
      ids.add(new Identifier(id));
    }
    SelectFeatureCommand cmd = new SelectFeatureCommand(layer, ids, false);
    try {
      appContainer.getCommandProcessor().executeSychronously(cmd, true);
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      throw new ModuleException(e.getMessage());
    }

    // get selected features as FeatureCollection and so their common bounding box
    FeatureCollection fc = layer.getSelectedFeatures();
    if (fc.size() == 0) {
      throw new ModuleException(Messages.get("$DG10094", paramater.get("OBJECTIDS")));
    }
    Envelope env = null;
    try {
      env = fc.getBoundedBy();
      if (env.getWidth() < 0.0001) {
        // feature collection just contains one point; set fix bbox width/height
        // 25 map units
        env = env.getBuffer(25);
      }
    } catch (GeometryException e) {
      LOG.logError(e.getMessage(), e);
      throw new ModuleException(e.getMessage());
    }

    // zoom to common bounding box of selected features
    ZoomCommand zcmd = new ZoomCommand(mapModel);
    zcmd.setZoomBox(
        env,
        mapModel.getTargetDevice().getPixelWidth(),
        mapModel.getTargetDevice().getPixelHeight());
    if ("TRUE".equalsIgnoreCase(paramater.get("sync"))) {
      try {
        appContainer.getCommandProcessor().executeSychronously(zcmd, true);
      } catch (Exception e) {
        LOG.logError(e.getMessage(), e);
        throw new ModuleException(e.getMessage());
      }
    } else {
      appContainer.getCommandProcessor().executeASychronously(zcmd);
    }

    return "feature selected";
  }
Пример #3
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();
    }
  }