Exemplo n.º 1
0
  private JSONObject getPrintJSON(ActionParameters params) throws ActionException {
    final JSONObject jsonprint = new JSONObject();
    try {
      final HttpServletRequest httpRequest = params.getRequest();

      // copy parameters
      JSONObject jsparamdata = new JSONObject();
      for (Object key : httpRequest.getParameterMap().keySet()) {
        String keyStr = (String) key;
        // not geojson param
        if (!keyStr.equals(PARM_GEOJSON) && !keyStr.equals(PARM_TILES)) {
          jsparamdata.put(keyStr, params.getHttpParam(keyStr));
        }
      }
      jsonprint.put(KEY_MAPLINK, jsparamdata);

      // construct state
      final JSONObject jsonstatedata = new JSONObject();

      final String[] coords = CoordinateParamHandler.parseParam(params.getHttpParam(PARM_COORD));
      if (coords.length == 2) {
        try {
          final double east = ConversionHelper.getDouble(coords[0], -1);
          final double north = ConversionHelper.getDouble(coords[1], -1);
          if (east == -1 || north == -1) {
            throw new IllegalArgumentException(
                "Coordinates not set: " + params.getHttpParam(PARM_COORD));
          }
          jsonstatedata.put(KEY_EAST, east);
          jsonstatedata.put(KEY_NORTH, north);
        } catch (Exception ex) {
          throw new ActionException("Could not set coordinates from URL param.", ex);
        }
      }
      jsonstatedata.put(KEY_ZOOM, ConversionHelper.getInt(params.getHttpParam(PARM_ZOOMLEVEL), 10));

      final String[] layers = params.getHttpParam(PARM_MAPLAYERS).split(",");
      final JSONArray configLayers = new JSONArray();
      final JSONArray selectedlayers = new JSONArray();

      // final String referer =
      // RequestHelper.getDomainFromReferer(params.getHttpHeader("Referer"));
      for (String layerString : layers) {
        final String[] layerProps = layerString.split(" ");
        final JSONObject layer =
            LayersParamHandler.getLayerJson(layerProps, "paikkatietoikkuna.fi");
        if (layer != null) {
          selectedlayers.put(layer);
          configLayers.put(layer);
        }
      }

      // GeoJson graphics layers to selected layers
      final String geojs64 = params.getHttpParam(PARM_GEOJSON, "");

      JSONArray geojs = null;
      if (!geojs64.isEmpty()) {
        // decoding geojson
        byte[] decoded = Base64.decodeBase64(geojs64.getBytes());

        geojs = new JSONArray(new String(decoded));

        JSONArray jslays = getGeojsonLayers(geojs);
        for (int i = 0; i < jslays.length(); i++) {
          selectedlayers.put(jslays.getJSONObject(i));
        }
      }
      jsonstatedata.put(KEY_SELECTEDLAYERS, selectedlayers);
      jsonprint.put(KEY_STATE, jsonstatedata);

      // printservice uses direct urls to myplaces instead of servletfilter/actionroute proxy
      final boolean useDirectURLForMyplaces = true;
      // populate layer details
      final JSONArray fullLayersConfigJson =
          MapfullHandler.getFullLayerConfig(
              configLayers,
              params.getUser(),
              params.getLocale().getLanguage(),
              params.getClientIp(),
              PRINT_VIEW,
              ViewTypes.PRINT,
              false,
              useDirectURLForMyplaces);

      // GeoJson graphics layers + styles
      if (geojs != null) {
        // Add geojson geometry and styles to layers section
        for (int i = 0; i < geojs.length(); i++) {
          fullLayersConfigJson.put(geojs.getJSONObject(i));
        }
      }

      // Add tiles, (statslayer, wfs)
      //      if (fullLayersConfigJson.toString().contains(VALUE_STATSLAYER)) {
      // Tiles in params ?
      if (!params.getHttpParam(PARM_TILES, "").isEmpty()) {

        JSONArray tiles = getTilesJSON(params);
        addTiles2Layers(fullLayersConfigJson, tiles);
      }
      //    }

      jsonprint.put(KEY_LAYERS, fullLayersConfigJson);

    } catch (Exception e) {
      throw new ActionException("Failed to create image", e);
    }

    return jsonprint;
  }