Exemplo n.º 1
0
  @Override
  public boolean action(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    String formactionsave = request.getParameter("formactionsave");
    String formactioncancel = request.getParameter("formactioncancel");
    String formactionsac = request.getParameter("formactionsac");

    if (isActionSelected(formactionsave)) {
      SaveAction sa = new SaveAction();
      if (sa.action(context)) {
        sa.render(context);
      }
      return false;
    }

    if (isActionSelected(formactioncancel)) {
      CancelAction ca = new CancelAction();
      if (ca.action(context)) {
        ca.render(context);
      }
      return false;
    }

    if (isActionSelected(formactionsac)) {
      SaveAndContinueAction saca = new SaveAndContinueAction();
      if (saca.action(context)) {
        saca.render(context);
      }
      return false;
    }
    return true;
  }
  @Override
  public boolean action(XWikiContext context) throws XWikiException {
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    XWikiRequest request = context.getRequest();

    String className = request.getParameter("classname");
    String objectNumber = request.getParameter("object");

    if (className != null && objectNumber != null) {
      try {
        BaseObject object = doc.getObject(className, Integer.valueOf(objectNumber));
        synchronizeObject(object, context);
      } catch (Exception ex) {
        // Wrong parameters, non-existing object
        return true;
      }
    } else {
      for (List<BaseObject> classObjects : doc.getXObjects().values()) {
        for (BaseObject object : classObjects) {
          synchronizeObject(object, context);
        }
      }
    }

    // Set the new author
    doc.setAuthorReference(context.getUserReference());

    xwiki.saveDocument(
        doc,
        localizePlainOrKey("core.model.xobject.synchronizeObjects.versionSummary"),
        true,
        context);

    if (Utils.isAjaxRequest(context)) {
      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
      response.setContentLength(0);
    } else {
      // forward to edit
      String redirect = Utils.getRedirect("edit", "editor=object", context);
      sendRedirect(response, redirect);
    }
    return false;
  }
  /**
   * Set the response HTTP headers common to both partial (Range) and full responses.
   *
   * @param attachment the attachment to get content from
   * @param request the current client request
   * @param response the response to write to.
   * @param context the current request context
   */
  private static void setCommonHeaders(
      final XWikiAttachment attachment,
      final XWikiRequest request,
      final XWikiResponse response,
      final XWikiContext context) {
    // Choose the right content type
    String mimetype = attachment.getMimeType(context);
    response.setContentType(mimetype);
    try {
      response.setCharacterEncoding("");
    } catch (IllegalCharsetNameException ex) {
      response.setCharacterEncoding(XWiki.DEFAULT_ENCODING);
    }

    String ofilename = Util.encodeURI(attachment.getFilename(), context).replaceAll("\\+", "%20");

    // The inline attribute of Content-Disposition tells the browser that they should display
    // the downloaded file in the page (see http://www.ietf.org/rfc/rfc1806.txt for more
    // details). We do this so that JPG, GIF, PNG, etc are displayed without prompting a Save
    // dialog box. However, all mime types that cannot be displayed by the browser do prompt a
    // Save dialog box (exe, zip, xar, etc).
    String dispType = "inline";

    // Determine whether the user who attached the file has Programming Rights or not.
    boolean hasPR = false;
    String author = attachment.getAuthor();
    try {
      hasPR =
          context
              .getWiki()
              .getRightService()
              .hasAccessLevel("programming", author, "XWiki.XWikiPreferences", context);
    } catch (Exception e) {
      hasPR = false;
    }
    // If the mimetype is not authorized to be displayed inline, let's force its content disposition
    // to download.
    if ((!hasPR && !isAuthorized(mimetype)) || "1".equals(request.getParameter("force-download"))) {
      dispType = ATTACHMENT;
    }
    // Use RFC 2231 for encoding filenames, since the normal HTTP headers only allows ASCII
    // characters.
    // See http://tools.ietf.org/html/rfc2231 for more details.
    response.addHeader("Content-disposition", dispType + "; filename*=utf-8''" + ofilename);

    response.setDateHeader("Last-Modified", attachment.getDate().getTime());
    // Advertise that downloads can be resumed
    response.setHeader("Accept-Ranges", "bytes");
  }
  @Override
  public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    String path = request.getRequestURI();
    String filename = Util.decodeURI(getFileName(path, ACTION_NAME), context);
    XWikiAttachment attachment = null;

    final String idStr = request.getParameter("id");
    if (StringUtils.isNumeric(idStr)) {
      int id = Integer.parseInt(idStr);
      if (doc.getAttachmentList().size() > id) {
        attachment = doc.getAttachmentList().get(id);
      }
    } else {
      attachment = doc.getAttachment(filename);
    }

    if (attachment == null) {
      Object[] args = {filename};
      throw new XWikiException(
          XWikiException.MODULE_XWIKI_APP,
          XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND,
          "Attachment {0} not found",
          null,
          args);
    }

    XWikiPluginManager plugins = context.getWiki().getPluginManager();
    attachment = plugins.downloadAttachment(attachment, context);

    // Try to load the attachment content just to make sure that the attachment really exists
    // This will throw an exception if the attachment content isn't available
    try {
      attachment.getContentSize(context);
    } catch (XWikiException e) {
      Object[] args = {filename};
      throw new XWikiException(
          XWikiException.MODULE_XWIKI_APP,
          XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND,
          "Attachment content {0} not found",
          null,
          args);
    }

    long lastModifiedOnClient = request.getDateHeader("If-Modified-Since");
    long lastModifiedOnServer = attachment.getDate().getTime();
    if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) {
      response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
      return null;
    }

    // Sending the content of the attachment
    if (request.getHeader(RANGE_HEADER_NAME) != null) {
      try {
        if (sendPartialContent(attachment, request, response, context)) {
          return null;
        }
      } catch (IOException ex) {
        // Broken response...
      }
    }
    sendContent(attachment, request, response, filename, context);
    return null;
  }
Exemplo n.º 5
0
 @Override
 public void readRequest() {
   XWikiRequest request = getRequest();
   setContent(request.getParameter("content"));
   setWeb(request.getParameter("web"));
   setName(request.getParameter("name"));
   setParent(request.getParameter("parent"));
   setTemplate(request.getParameter("template"));
   setDefaultTemplate(request.getParameter("default_template"));
   setCreator(request.getParameter("creator"));
   setLanguage(request.getParameter("language"));
   setTitle(request.getParameter("title"));
   setComment(request.getParameter("comment"));
   setDefaultLanguage(request.getParameter("defaultLanguage"));
   setTags(request.getParameterValues("tags"));
   setLockForce("1".equals(request.getParameter("force")));
   setMinorEdit(request.getParameter("minorEdit") != null);
   setSyntaxId(request.getParameter("syntaxId"));
   setHidden(request.getParameter("xhidden"));
 }