예제 #1
0
  public void updateShare(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    try {
      request = wrapRequest(request);
      User user = securityService.getCurrentUser(request);
      int id = ServletRequestUtils.getRequiredIntParameter(request, "id");

      Share share = shareService.getShareById(id);
      if (share == null) {
        error(request, response, ErrorCode.NOT_FOUND, "Shared media not found.");
        return;
      }
      if (!user.isAdminRole() && !share.getUsername().equals(user.getUsername())) {
        error(
            request, response, ErrorCode.NOT_AUTHORIZED, "Not authorized to modify shared media.");
        return;
      }

      share.setDescription(request.getParameter("description"));
      String expiresString = request.getParameter("expires");
      if (expiresString != null) {
        long expires = Long.parseLong(expiresString);
        share.setExpires(expires == 0L ? null : new Date(expires));
      }
      shareService.updateShare(share);
      XMLBuilder builder = createXMLBuilder(request, response, true).endAll();
      response.getWriter().print(builder);

    } catch (ServletRequestBindingException x) {
      error(request, response, ErrorCode.MISSING_PARAMETER, getErrorMessage(x));
    } catch (Exception x) {
      LOG.warn("Error in REST API.", x);
      error(request, response, ErrorCode.GENERIC, getErrorMessage(x));
    }
  }
예제 #2
0
  public void createShare(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    request = wrapRequest(request);
    Player player = playerService.getPlayer(request, response);

    User user = securityService.getCurrentUser(request);
    if (!user.isShareRole()) {
      error(
          request,
          response,
          ErrorCode.NOT_AUTHORIZED,
          user.getUsername() + " is not authorized to share media.");
      return;
    }

    XMLBuilder builder = createXMLBuilder(request, response, true);

    try {

      List<MediaFile> files = new ArrayList<MediaFile>();
      for (String id : ServletRequestUtils.getRequiredStringParameters(request, "id")) {
        MediaFile file = mediaFileService.getMediaFile(NumberUtils.toInt(id));
        files.add(file);
      }

      // TODO: Update api.jsp

      Share share = shareService.createShare(request, files);
      share.setDescription(request.getParameter("description"));
      long expires = ServletRequestUtils.getLongParameter(request, "expires", 0L);
      if (expires != 0) {
        share.setExpires(new Date(expires));
      }
      shareService.updateShare(share);

      builder.add("shares", false);
      builder.add("share", createAttributesForShare(share), false);

      for (MediaFile mediaFile : shareService.getSharedFiles(share.getId())) {
        File coverArt = mediaFileService.getCoverArt(mediaFile);
        AttributeSet attributes =
            restBrowseController.createAttributesForMediaFile(player, coverArt, mediaFile);
        builder.add("entry", attributes, true);
      }

      builder.endAll();
      response.getWriter().print(builder);

    } catch (ServletRequestBindingException x) {
      error(request, response, ErrorCode.MISSING_PARAMETER, getErrorMessage(x));
    } catch (Exception x) {
      LOG.warn("Error in REST API.", x);
      error(request, response, ErrorCode.GENERIC, getErrorMessage(x));
    }
  }
  private String handleParameters(HttpServletRequest request) {
    User user = securityService.getCurrentUser(request);
    for (Share share : shareService.getSharesForUser(user)) {
      int id = share.getId();

      String description = getParameter(request, "description", id);
      boolean delete = getParameter(request, "delete", id) != null;
      String expireIn = getParameter(request, "expireIn", id);

      if (delete) {
        shareService.deleteShare(id);
      } else {
        if (expireIn != null) {
          share.setExpires(parseExpireIn(expireIn));
        }
        share.setDescription(description);
        shareService.updateShare(share);
      }
    }

    return null;
  }