Ejemplo n.º 1
0
  /*
   * Create a new data source (metadata entry).
   * If there is another one with the same identifier, then - error.
   */
  @RequestMapping(
      value = "/admin/datasources",
      consumes = "application/json",
      method = RequestMethod.PUT)
  public void put(
      @RequestBody @Valid Metadata metadata,
      BindingResult bindingResult,
      HttpServletResponse response)
      throws IOException {
    if (bindingResult != null && bindingResult.hasErrors()) {
      log.error(
          Status.BAD_REQUEST.getErrorCode()
              + "; "
              + Status.BAD_REQUEST.getErrorMsg()
              + "; "
              + errorFromBindingResult(bindingResult));
      response.sendError(
          Status.BAD_REQUEST.getErrorCode(),
          Status.BAD_REQUEST.getErrorMsg() + "; " + errorFromBindingResult(bindingResult));
    }

    Metadata existing = service.metadata().findByIdentifier(metadata.identifier);
    if (existing == null) {
      service.save(metadata);
    } else {
      response.sendError(
          Status.BAD_REQUEST.getErrorCode(),
          "PUT failed: Metadata already exists for pk: "
              + metadata.identifier
              + " (use POST to update instead)");
    }
  }
Ejemplo n.º 2
0
  /*
   * Update a data source definition (metadata entry)
   */
  @RequestMapping(
      value = "/admin/datasources",
      consumes = "application/json",
      method = RequestMethod.POST)
  public void update(
      @RequestBody @Valid Metadata metadata,
      BindingResult bindingResult,
      HttpServletResponse response)
      throws IOException {
    if (bindingResult != null && bindingResult.hasErrors()) {
      log.error(
          Status.BAD_REQUEST.getErrorCode()
              + "; "
              + Status.BAD_REQUEST.getErrorMsg()
              + "; "
              + errorFromBindingResult(bindingResult));
      response.sendError(
          Status.BAD_REQUEST.getErrorCode(),
          Status.BAD_REQUEST.getErrorMsg() + "; " + errorFromBindingResult(bindingResult));
    }

    service.save(metadata);
  }
Ejemplo n.º 3
0
  // Upload (and replace if exists) a data archive for the existing data source
  @RequestMapping(value = "/admin/datasources/{identifier}/file", method = RequestMethod.POST)
  public void uploadDataArchive(
      @PathVariable String identifier,
      MultipartHttpServletRequest multiRequest,
      HttpServletResponse response)
      throws IOException {
    Metadata m = service.metadata().findByIdentifier(identifier);
    if (m == null) {
      response.sendError(
          Status.NO_RESULTS_FOUND.getErrorCode(),
          Status.NO_RESULTS_FOUND.getErrorMsg()
              + "; Metadata object with identifier: "
              + identifier
              + " not found.");
    }

    Map<String, MultipartFile> files = multiRequest.getFileMap();
    Assert.state(!files.isEmpty(), "No files to validate");
    String filename = files.keySet().iterator().next();
    MultipartFile file = files.get(filename);
    String origFilename = file.getOriginalFilename();
    if (file.getBytes().length == 0
        || filename == null
        || "".equals(filename)
        || !origFilename.endsWith(".zip")) {
      log.error("uploadDataArchive(), empty data file or null: " + origFilename);
      response.sendError(
          Status.BAD_REQUEST.getErrorCode(),
          "File (" + origFilename + ") UPLOAD failed; id:" + identifier);
    } else {
      // create or update the input source data file (must be ZIP archive!)
      CPathUtils.write(file.getBytes(), m.getDataArchiveName());
      log.info(
          "uploadDataArchive(), saved uploaded file:"
              + origFilename
              + " as "
              + m.getDataArchiveName());
    }
  }