public String buildContentTypeXML(ExtendedMap formItems, boolean reload) {
    StringWriter sw = new StringWriter();

    try {
      Document doc = XMLTool.createDocument();

      // Create unit element
      Element contentType = XMLTool.createRootElement(doc, "contenttype");

      if (formItems.containsKey("key")) {
        contentType.setAttribute("key", formItems.getString("key"));
      }
      if (formItems.containsKey("sitekey")) {
        contentType.setAttribute("sitekey", formItems.getString("sitekey"));
      }
      if (formItems.containsKey("contenthandlerkey")) {
        contentType.setAttribute("contenthandlerkey", formItems.getString("contenthandlerkey"));
      }
      if (formItems.containsKey("csskey")) {
        contentType.setAttribute("csskey", formItems.getString("csskey"));
      }

      XMLTool.createElement(doc, contentType, "name", formItems.getString("name", ""));
      XMLTool.createElement(
          doc, contentType, "description", formItems.getString("description", ""));

      // Module XML
      String moduleXML = formItems.getString("module", "");
      if (moduleXML == null || moduleXML.length() == 0) {
        XMLTool.createElement(doc, contentType, "moduledata");
      } else {
        if (!reload) {
          try {
            Document modDocTemp = XMLTool.domparse(moduleXML, "contenttype");

            Document modDoc = XMLTool.createDocument("moduledata");
            XMLTool.mergeDocuments(modDoc, modDocTemp, false);

            contentType.appendChild(doc.importNode(modDoc.getDocumentElement(), true));
          } catch (Exception e) {
            addError(2, "module", moduleXML);
          }
        }
      }
      XMLTool.printDocument(sw, doc);
    } catch (Exception e) {
      System.err.println("[Error] Something failed:\n" + e.toString());
    }
    return sw.toString();
  }
  public void handlerCustom(
      HttpServletRequest request,
      HttpServletResponse response,
      HttpSession session,
      AdminService admin,
      ExtendedMap formItems,
      String operation,
      ExtendedMap parameters,
      User user,
      Document verticalDoc)
      throws VerticalAdminException, VerticalEngineException, MessagingException, IOException {

    if (operation.equals("insert")) {
      int categoryKey = formItems.getInt("cat");
      int unitKey = admin.getUnitKey(categoryKey);
      int page = formItems.getInt("page");

      ExtendedMap xslParams = new ExtendedMap();
      xslParams.put("page", String.valueOf(page));

      Document newDoc =
          admin
              .getContent(user, Integer.parseInt(formItems.getString("key")), 0, 1, 0)
              .getAsDOMDocument();
      if (newDoc != null) {
        String filename = XMLTool.getElementText(newDoc, "/contents/content/contentdata/name");
        int index = filename.lastIndexOf(".");
        if (index != -1) {
          String filetype = filename.substring(index + 1).toLowerCase();
          if ("swf".equals(filetype)) {
            xslParams.put("flash", "true");
          } else if ("jpg".equals(filetype)
              || "jpeg".equals(filetype)
              || "png".equals(filetype)
              || "gif".equals(filetype)) {
            xslParams.put("image", "true");
          }
        }
      }

      Document xmlCategory =
          admin.getSuperCategoryNames(categoryKey, false, true).getAsDOMDocument();
      XMLTool.mergeDocuments(newDoc, xmlCategory, true);

      addCommonParameters(admin, user, request, xslParams, unitKey, -1);

      if (formItems.containsKey("subop")) {
        xslParams.put("subop", formItems.getString("subop"));
      }

      transformXML(request, response, newDoc, "editor/" + "imagepopup_selected.xsl", xslParams);
    } else {
      super.handlerCustom(
          request, response, session, admin, formItems, operation, parameters, user, verticalDoc);
    }
  }
Example #3
0
  /**
   * Process incoming requests for information
   *
   * @param request Object that encapsulates the request to the servlet
   * @param response Object that encapsulates the response from the servlet
   */
  private void performTask(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session;
    session = request.getSession(false);
    response.setContentType("text/html;charset=UTF-8");

    // Make IE 9 behave like IE 8
    // http://msdn.microsoft.com/en-us/library/cc288325%28v=vs.85%29.aspx#Servers
    response.setHeader("X-UA-Compatible", "IE=EmulateIE8");

    if (session == null) {
      VerticalAdminLogger.debug(
          this.getClass(), 100, "Session is null. Redirecting to login.", null);

      // failed to get session, redirect to login page
      try {
        redirectClientToAdminPath("login", (MultiValueMap) null, request, response);
      } catch (VerticalAdminException vae) {
        String message = "Failed to redirect to login page: %t";
        VerticalAdminLogger.fatalAdmin(this.getClass(), 0, message, vae);
      }
    } else {
      // lookup admin bean
      AdminService admin = lookupAdminBean();
      User user = securityService.getLoggedInAdminConsoleUser();
      if (user == null) {
        // no logged in user, invalidate session and redirect to login page
        String message = "No user logged in. Redirecting to login.";
        VerticalAdminLogger.debug(this.getClass(), 0, message, null);
        try {
          redirectClientToAdminPath("login", (MultiValueMap) null, request, response);
        } catch (VerticalAdminException vae) {
          message = "Failed to redirect to login page: %t";
          VerticalAdminLogger.fatalAdmin(this.getClass(), 0, message, vae);
        }
      } else {
        response.setContentType("text/html; charset=UTF-8");
        try {
          ExtendedMap formItems = parseForm(request, false);

          String operation;
          if (formItems.containsKey("op")) {
            operation = formItems.getString("op");
          } else {
            operation = request.getParameter("op");
          }

          // Common parameters and variables
          ExtendedMap parameters = new ExtendedMap();
          int unitKey = formItems.getInt("selectedunitkey", -1);
          int menuKey = formItems.getInt("selectedmenukey", -1);
          int page = formItems.getInt("page", -1);
          if (page == 993) {
            int contentKey = -1;

            String contentKeyStr = request.getParameter("key");
            if (contentKeyStr != null) {
              contentKey = Integer.parseInt(contentKeyStr);
            }

            if (contentKey == -1) {
              String versionKeyStr = request.getParameter("versionkey");
              if (versionKeyStr != null) {
                int versionKey = Integer.parseInt(versionKeyStr);
                contentKey = admin.getContentKeyByVersionKey(versionKey);
              }
            }

            if (contentKey != -1) {
              int contentTypeKey = admin.getContentTypeKey(contentKey);
              page = contentTypeKey + 999;
              formItems.put("page", page);
            }
          }

          if (page == 991) {
            int categoryKey = formItems.getInt("categorykey", -1);
            if (categoryKey == -1) {
              categoryKey = formItems.getInt("cat", -1);
            }

            if (categoryKey != -1) {
              int contentTypeKey = admin.getContentTypeKeyByCategory(categoryKey);
              if (contentTypeKey != -1) {
                page = contentTypeKey + 999;
              }
            }
          }
          parameters.put("page", Integer.toString(page));
          addCommonParameters(admin, user, request, parameters, unitKey, menuKey);
          Document verticalDoc = XMLTool.createDocument("data");

          if ("create".equals(operation)) {
            handlerCreate(request, response, session, admin, formItems);
          } else if ("update".equals(operation)) {
            handlerUpdate(request, response, session, admin, formItems);
          } else if ("remove".equals(operation)) {
            String keyStr = request.getParameter("key");
            if (StringUtil.isIntegerString(keyStr)) {
              int key = -1;
              try {
                key = Integer.parseInt(keyStr);
              } catch (NumberFormatException nfe) {
                String message = "Failed to parse key (%0): %t";
                VerticalAdminLogger.errorAdmin(this.getClass(), 5, message, keyStr, nfe);
              }
              handlerRemove(request, response, session, admin, formItems, key);
            } else {
              handlerRemove(request, response, session, admin, formItems, keyStr);
            }
          } else if ("copy".equals(operation)) {
            String keyStr = request.getParameter("key");
            int key = -1;
            try {
              key = Integer.parseInt(keyStr);
            } catch (NumberFormatException nfe) {
              String message = "Failed to parse key (%0): %t";
              VerticalAdminLogger.errorAdmin(this.getClass(), 5, message, keyStr, nfe);
            }

            handlerCopy(request, response, session, admin, formItems, user, key);
          } else if ("import".equals(operation)) {
            throw new IllegalArgumentException("Unsupported operation: import");
          } else if ("browse".equals(operation)) {
            handlerBrowse(
                request, response, session, admin, formItems, parameters, user, verticalDoc);
          } else if ("select".equals(operation)) {
            handlerSelect(request, response, session, admin, formItems);
          } else if ("show".equals(operation)) {
            handlerShow(request, response, session, admin, formItems);
          } else if ("form".equals(operation)) {
            this.clearErrors();
            handlerForm(request, response, session, admin, formItems);
          } else if ("searchform".equals(operation)) {
            handlerSearch(request, response, session, admin, formItems);
          } else if ("searchresults".equals(operation)) {
            handlerSearchResults(request, response, session, admin, formItems);
          } else if ("report".equals(operation)) {
            String subOp = formItems.getString("subop");
            handlerReport(request, response, session, admin, formItems, subOp);
          } else if ("closewindow".equals(operation)) {
            closeWindow(response);
          } else if ("preview".equals(operation)) {
            handlerPreview(request, response, session, admin, formItems);
          } else if ("menu".equals(operation)) {
            handlerMenu(
                request, response, session, admin, formItems, parameters, user, verticalDoc);
          } else if ("notify".equals(operation)) {
            handlerNotify(request, response, session, admin, formItems, user);
          } else if ("wizard".equals(operation)) {
            String wizardName = formItems.getString("name");
            handlerWizard(
                request, response, session, admin, formItems, parameters, user, wizardName);
          } else if (operation != null) {
            handlerCustom(
                request,
                response,
                session,
                admin,
                formItems,
                operation,
                parameters,
                user,
                verticalDoc);
          } else {
            handlerCustom(request, response, session, admin, formItems, "missing");
          }
        } catch (Exception e) {
          try {
            if (!(e instanceof VerticalException) && !(e instanceof VerticalRuntimeException)) {
              String message = "Unexpected error occurred during handling of admin page: %t";
              VerticalAdminLogger.error(this.getClass(), 8, message, e);
            }
            ErrorPageServlet.Error error = new ErrorPageServlet.ThrowableError(e);
            session.setAttribute("com.enonic.vertical.error", error);
            redirectClientToAdminPath("errorpage", (MultiValueMap) null, request, response);
          } catch (VerticalAdminException vae) {
            String message = "Failed to redirect to error page: %t";
            VerticalAdminLogger.fatalAdmin(this.getClass(), 0, message, vae);
          }
        }
      }
    }
  }
Example #4
0
  private ExtendedMap parseMultiPartRequest(HttpServletRequest request)
      throws FileUploadException, IOException {
    ExtendedMap formItems = new ExtendedMap();
    List paramList = fileUpload.parseRequest(request);
    for (Object parameter : paramList) {
      FileItem fileItem = (FileItem) parameter;

      String name = fileItem.getFieldName();

      if (fileItem.isFormField()) {
        String value = fileItem.getString("UTF-8");
        if (formItems.containsKey(name)) {
          ArrayList<Object> values = new ArrayList<Object>();
          Object obj = formItems.get(name);
          if (obj instanceof Object[]) {
            String[] objArray = (String[]) obj;
            for (int i = 0; i < objArray.length; i++) {
              values.add(objArray[i]);
            }
          } else {
            values.add(obj);
          }
          values.add(value);
          formItems.put(name, values.toArray(new String[values.size()]));
        } else {
          formItems.put(name, value);
        }
      } else {
        if (fileItem.getSize() > 0) {
          if (formItems.containsKey(name)) {
            ArrayList<Object> values = new ArrayList<Object>();
            Object obj = formItems.get(name);
            if (obj instanceof FileItem[]) {
              FileItem[] objArray = (FileItem[]) obj;
              for (int i = 0; i < objArray.length; i++) {
                values.add(objArray[i]);
              }
            } else {
              values.add(obj);
            }
            values.add(fileItem);
            formItems.put(name, values.toArray(new FileItem[values.size()]));
          } else {
            formItems.put(name, fileItem);
          }
        }
      }
    }

    // Add parameters from url
    Map paramMap = URLUtil.decodeParameterMap(request.getParameterMap());
    for (Object parameterEntry : paramMap.entrySet()) {
      Map.Entry entry = (Map.Entry) parameterEntry;
      String key = (String) entry.getKey();
      String[] values = (String[]) entry.getValue();
      for (String value : values) {
        formItems.put(key, value);
      }
    }

    // Remove all empty parameters that are NOT in an array
    ArrayList<String> remove = new ArrayList<String>();
    for (Object parameterKey : formItems.keySet()) {
      String key = (String) parameterKey;
      Object value = formItems.get(key);
      if (!(value instanceof String[])
          && value instanceof String
          && ((String) value).length() == 0) {
        remove.add(key);
      }
    }
    for (String key : remove) {
      formItems.remove(key);
    }

    return formItems;
  }
Example #5
0
  protected Document buildChangedAccessRightsXML(ExtendedMap formItems) {

    Document doc = XMLTool.createDocument("changedaccessrights");
    Element changed = doc.getDocumentElement();

    // Lager xml for diffen - de som er fjernet
    for (Object parameterKey : formItems.keySet()) {

      String paramName = (String) parameterKey;
      if (paramName.startsWith("original_accessright[key=")) {

        ExtendedMap paramsInName = ParamsInTextParser.parseParamsInText(paramName, "[", "]", ";");
        String key = paramsInName.getString("key");

        if (!formItems.containsKey("accessright[key=" + key + "]")) {
          String paramValue = formItems.getString(paramName);
          ExtendedMap paramsInValue =
              ParamsInTextParser.parseParamsInText(paramValue, "[", "]", ";");
          Element elmAccessRight = buildAccessRightElement(doc, changed, key, paramsInValue);
          elmAccessRight.setAttribute("diffinfo", "removed");
        }
      }
    }
    // Lager xml for diffen - de som er lagt til
    for (Object parameterKey : formItems.keySet()) {
      String paramName = (String) parameterKey;
      if (paramName.startsWith("accessright[key=")) {

        ExtendedMap paramsInName = ParamsInTextParser.parseParamsInText(paramName, "[", "]", ";");
        String key = paramsInName.getString("key");

        if (!formItems.containsKey("original_accessright[key=" + key + "]")) {

          String paramValue = formItems.getString(paramName);
          ExtendedMap paramsInValue =
              ParamsInTextParser.parseParamsInText(paramValue, "[", "]", ";");

          Element elmAccessRight = buildAccessRightElement(doc, changed, key, paramsInValue);
          elmAccessRight.setAttribute("diffinfo", "added");
        }
      }
    }
    // Lager xml for diffen - de som er endret
    for (Object paramKey : formItems.keySet()) {

      String paramName = (String) paramKey;

      if (paramName.startsWith("accessright[key=")) {

        ExtendedMap paramsInName = ParamsInTextParser.parseParamsInText(paramName, "[", "]", ";");
        String key = paramsInName.getString("key");

        if (formItems.containsKey("original_accessright[key=" + key + "]")) {

          String originalValue = formItems.getString("original_accessright[key=" + key + "]");
          String currentValue = formItems.getString(paramName);
          if (!currentValue.equals(originalValue)) {

            ExtendedMap paramsInValue =
                ParamsInTextParser.parseParamsInText(currentValue, "[", "]", ";");
            Element elmAccessRight = buildAccessRightElement(doc, changed, key, paramsInValue);
            elmAccessRight.setAttribute("diffinfo", "modified");
          }
        }
      }
    }

    return doc;
  }
Example #6
0
 public void addFeedback(Document doc, ExtendedMap formItems) {
   if (formItems.containsKey("feedback")) {
     addFeedback(doc, formItems.getInt("feedback"));
   }
 }
  public void handlerForm(
      HttpServletRequest request,
      HttpServletResponse response,
      HttpSession session,
      AdminService admin,
      ExtendedMap formItems)
      throws VerticalAdminException {

    boolean createContent = formItems.containsKey("create");
    boolean reloading = formItems.getString("reload", "").equals("true");
    boolean usehandlerindexing = false;
    HashMap<String, String> parameters = new HashMap<String, String>();
    Element contentTypeElem = null;
    Document xmlData = null;
    Document doc = null;
    String referer = request.getHeader("referer");

    int contentTypeKey = -1;
    String generateKey = formItems.getString("generatectykey", "");
    if (generateKey.equals("true")) {
      parameters.put("generatectykey", "true");
    } else {
      String keyStr = (String) request.getParameter("key");
      if (keyStr != null && keyStr.length() > 0) {
        contentTypeKey = Integer.parseInt(keyStr);
      }
    }

    int contentHandlerKey = formItems.getInt("contenthandlerkey", 0);

    if (reloading || hasErrors()) {
      if (reloading) {
        usehandlerindexing = true;
      }

      doc = XMLTool.createDocument("contenttypes");
      Document contentTypeDoc = XMLTool.domparse(buildContentTypeXML(formItems, reloading));
      contentTypeElem = contentTypeDoc.getDocumentElement();
      if (formItems.containsKey("key")) {
        int key = formItems.getInt("key");
        int contentCount = admin.getContentCountByContentType(key);
        contentTypeElem.setAttribute("contentcount", String.valueOf(contentCount));
      }
      doc.getDocumentElement().appendChild(doc.importNode(contentTypeElem, true));

      referer = formItems.getString("referer");
    } else if (contentTypeKey == -1) {
      // Blank form, make dummy document
      doc = XMLTool.createDocument("contenttypes");

      usehandlerindexing = true;

      // Create content type element
      contentTypeElem = XMLTool.createElement(doc, doc.getDocumentElement(), "contenttype");
      createContent = true;
    } else {
      // Edit content type
      xmlData = admin.getContentType(contentTypeKey, true).getAsDOMDocument();

      doc = xmlData;

      contentTypeElem = XMLTool.getElement(doc.getDocumentElement(), "contenttype");
      String contentHandlerString = contentTypeElem.getAttribute("contenthandlerkey");
      if (contentHandlerString.length() > 0) {
        contentHandlerKey = Integer.parseInt(contentHandlerString);
      }
      String cssString = contentTypeElem.getAttribute("csskey");
      if (cssString.length() > 0) {
        ResourceKey cssKey = new ResourceKey(cssString);
        parameters.put("cssname", cssKey.toString());

        ResourceFile contentTypeStylesheet = this.resourceService.getResourceFile(cssKey);
        if (contentTypeStylesheet == null) {
          parameters.put("cssexist", "false");
        } else {
          parameters.put("cssexist", "true");
        }
      }
    }
    Document contentHandlersDoc = admin.getContentHandlers().getAsDOMDocument();
    doc.getDocumentElement()
        .appendChild(doc.importNode(contentHandlersDoc.getDocumentElement(), true));

    addErrorsXML(doc);

    if (createContent) {
      parameters.put("create", "1");
    } else {
      parameters.put("create", "0");
    }
    parameters.put("referer", referer);

    if (contentHandlerKey > -1) {
      parameters.put("contenthandlerkey", Integer.toString(contentHandlerKey));
    }
    parameters.put("usehandlerindexing", String.valueOf(usehandlerindexing));
    parameters.put("page", String.valueOf(request.getParameter("page").toString()));
    parameters.put("errorInConfig", formItems.getString("errorInConfig", ""));

    transformXML(request, response, doc, "contenttype_form.xsl", parameters);
  }