Ejemplo n.º 1
0
  /**
   * Forwards requests to resource.
   *
   * @param name
   * @param response
   * @param request
   * @param layers
   * @param center
   * @param zoom
   * @param size
   * @param format
   * @throws WmsException
   */
  @RequestMapping(
      value = "/{name}/redirect",
      method = RequestMethod.GET,
      produces = "application/json; charset=utf-8")
  public void forwardWmsMap(
      @PathVariable String name,
      HttpServletResponse response,
      HttpServletRequest request,
      @RequestParam String layers,
      @RequestParam(required = false) String center,
      @RequestParam(required = false) String zoom,
      @RequestParam(required = false) String size,
      @RequestParam(required = false) String format)
      throws WmsException {

    if (LOGGER.isDebugEnabled())
      LOGGER.debug(
          "Received params: \nlayers="
              + layers
              + "\ncenter="
              + center
              + "\nzoom="
              + zoom
              + "\nsize="
              + size
              + "\nformat="
              + format);

    WmsEntity wmsEntity = wmsRepository.findByName(name);
    if (wmsEntity == null) {
      // no WMS entity for given ID
      throw new HttpClientErrorException(
          HttpStatus.BAD_REQUEST, "No WMS entity for name given in URL; name=" + name);
    }

    WmsCapabilities capabilities = wmsService.getCapabilities(wmsEntity.getUrl());
    if (capabilities == null) {
      // no WMS entity for given ID
      throw new HttpClientErrorException(
          HttpStatus.FAILED_DEPENDENCY, "Incorrect answer from WMS resource");
    }
    LayerWrap availableLayers = capabilities.getCapability().getLayerWrap();

    try {
      center =
          center != null
              ? center
              : configService.getStringValueForConfigVariable("wms.params.center");
      zoom = zoom != null ? zoom : configService.getStringValueForConfigVariable("wms.params.zoom");
      size = size != null ? size : configService.getStringValueForConfigVariable("wms.params.size");
      format =
          format != null
              ? format
              : configService.getStringValueForConfigVariable("wms.params.format");

      validate(request, layers, center, zoom, size, format, availableLayers);

      String[] centerParts = center.split(",");
      String[] sizeParts = size.split("x");
      String url =
          getUrl(
              false,
              wmsEntity,
              capabilities,
              layers,
              Double.parseDouble(centerParts[0]),
              Double.parseDouble(centerParts[1]),
              Integer.parseInt(zoom),
              Integer.parseInt(sizeParts[0]),
              Integer.parseInt(sizeParts[1]),
              format);
      response.sendRedirect(url);
    } catch (IOException e) {
      LOGGER.error(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: IOException", e);
      throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (ConfigException e) {
      LOGGER.error(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: ConfigException", e);
      throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
  }
Ejemplo n.º 2
0
  /**
   * Get selected map encoded in Base64 code.
   *
   * @param name - resource name.
   * @param layers - coma separated list of selected layers.
   * @param center - coma separated coordinates of center point.
   * @param zoom - zoom value, default is 10.
   * @param size - size of returned image.
   * @param format - image format, ie. png
   * @return ResponseEntity with result.
   */
  @RequestMapping(
      value = "/{name}/getmap",
      method = RequestMethod.GET,
      produces = "application/json; charset=utf-8")
  public ResponseEntity getWmsMap(
      @PathVariable String name,
      @RequestParam String layers,
      @RequestParam(required = false) String center,
      @RequestParam(required = false) String zoom,
      @RequestParam(required = false) String size,
      @RequestParam(required = false) String format) {
    try {
      LOGGER.info("getWmsMap");
      WmsEntity wmsEntity = wmsRepository.findByName(name);
      Assert.notNull(wmsEntity, "No WMS entity for given name.");
      WmsCapabilities capabilities = wmsService.getCapabilities(wmsEntity.getUrl());

      if (LOGGER.isDebugEnabled())
        LOGGER.debug(
            "Received params: \nlayers="
                + layers
                + "\ncenter="
                + center
                + "\nzoom="
                + zoom
                + "\nsize="
                + size
                + "\nformat="
                + format);

      center =
          center != null
              ? center
              : configService.getStringValueForConfigVariable("wms.params.center");
      zoom = zoom != null ? zoom : configService.getStringValueForConfigVariable("wms.params.zoom");
      size = size != null ? size : configService.getStringValueForConfigVariable("wms.params.size");
      format =
          format != null
              ? format
              : configService.getStringValueForConfigVariable("wms.params.format");

      String[] centerParts = center.split(",");
      Assert.notNull(centerParts, "Param center has wrong format.");
      Assert.isTrue(centerParts.length == 2, "Param center has wrong format.");
      Assert.isInstanceOf(
          Double.class, Double.parseDouble(centerParts[0]), "Param center has wrong format.");
      Assert.isInstanceOf(
          Double.class, Double.parseDouble(centerParts[1]), "Param center has wrong format.");

      String[] sizeParts = size.split("x");
      Assert.notNull(sizeParts, "Param size has wrong format.");
      Assert.isTrue(sizeParts.length == 2, "Param size has wrong format.");
      Assert.isInstanceOf(
          Integer.class, Integer.parseInt(sizeParts[0]), "Param size has wrong format.");
      Assert.isInstanceOf(
          Integer.class, Integer.parseInt(sizeParts[1]), "Param size has wrong format.");

      Assert.isInstanceOf(Integer.class, Integer.parseInt(zoom), "Param zoom has wrong format.");

      Assert.isTrue((format.equals("png") || format.equals("gif")), "Param format is invalid.");

      String url =
          getUrl(
              true,
              wmsEntity,
              capabilities,
              layers,
              Double.parseDouble(centerParts[0]),
              Double.parseDouble(centerParts[1]),
              Integer.parseInt(zoom),
              Integer.parseInt(sizeParts[0]),
              Integer.parseInt(sizeParts[1]),
              format);

      // WmsApiResponse response = new WmsApiResponse();
      // response.setResult(wmsService.getMap(url));

      WmsApiBase64MapResponse response = new WmsApiBase64MapResponse();
      response.setBase64map(wmsService.getMap(url));

      return new ResponseEntity<>(response, HttpStatus.OK);
    } catch (NumberFormatException e) {
      LOGGER.error(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: NumberFormatException", e);
      return new ResponseEntity<>("NumberFormatException", HttpStatus.BAD_REQUEST);
    } catch (IllegalArgumentException e) {
      LOGGER.error(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: IllegalArgumentException", e);
      return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
    } catch (WmsException e) {
      LOGGER.error(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: WmsException", e);
      return new ResponseEntity<>(e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
    } catch (Exception e) {
      LOGGER.fatal(e);
      if (LOGGER.isDebugEnabled()) LOGGER.debug("WMS: Exception", e);
      return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }