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(); }
protected boolean logUserStoreLogin( User user, AdminService admin, String remoteIP, String remoteHost, UserStoreKey userStoreKey) { try { Document doc = XMLTool.createDocument("logentry"); Element rootElement = doc.getDocumentElement(); // UserStoreKey userStoreKey = user.getUserStoreKey(); if (userStoreKey != null) { rootElement.setAttribute("userstorekey", String.valueOf(userStoreKey)); } rootElement.setAttribute("sitekey", String.valueOf(0)); rootElement.setAttribute("typekey", String.valueOf(LogType.LOGIN_USERSTORE.asInteger())); rootElement.setAttribute("inetaddress", remoteIP); XMLTool.createElement(doc, rootElement, "data"); admin.createLogEntries(user, XMLTool.documentToString(doc)); } catch (VerticalSecurityException vse) { String message = "Failed to create log entry because of security error: %t"; VerticalAdminLogger.error(this.getClass(), 1, message, vse); return false; } return true; }
public static final Document buildAccessRightsXML( Element rootElem, String key, ExtendedMap formItems, int accessrightsType) { // Handle this in calling methods instead // if (!formItems.containsKey("updateaccessrights")) // return null; Document doc; Element elmAccessRights; if (rootElem != null) { doc = rootElem.getOwnerDocument(); elmAccessRights = XMLTool.createElement(doc, rootElem, "accessrights"); } else { doc = XMLTool.createDocument("accessrights"); elmAccessRights = doc.getDocumentElement(); } if (key != null) { elmAccessRights.setAttribute("key", key); } if (accessrightsType != Integer.MIN_VALUE) { elmAccessRights.setAttribute("type", String.valueOf(accessrightsType)); } for (Object parameterKey : formItems.keySet()) { String paramName = (String) parameterKey; if (paramName.startsWith("accessright[key=")) { String paramValue = formItems.getString(paramName); ExtendedMap paramsInName = ParamsInTextParser.parseParamsInText(paramName, "[", "]", ";"); ExtendedMap paramsInValue = ParamsInTextParser.parseParamsInText(paramValue, "[", "]", ";"); buildAccessRightElement(doc, elmAccessRights, paramsInName.getString("key"), paramsInValue); } } return doc; }
/** * 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); } } } } }
protected Document applyChangesInAccessRights( Document docExistingAccessRights, Map<String, ExtendedMap> removedAccessRights, Map<String, ExtendedMap> modifiedAccessRights, Map<String, ExtendedMap> addedAccessRights) { // We have to make a clone of this hashtable, because we may have to remove some elements // and we don't want to affect this on the original hashtable. addedAccessRights = new HashMap<String, ExtendedMap>(addedAccessRights); // ("removedAccessRights = " + removedAccessRights); // ("modifiedAccessRights = " + modifiedAccessRights); // ("addedAccessRights = " + addedAccessRights); Element elExistingAccessRights = docExistingAccessRights.getDocumentElement(); // ("antall accessrights: " + elExistingAccessRights.getChildNodes().getLength()); // Loop thru existing accessrights and check if there is anyone to remove or modify Element curAccessRight = (Element) elExistingAccessRights.getFirstChild(); while (curAccessRight != null) { String groupKey = curAccessRight.getAttribute("groupkey"); // ("checking accessright, groupkey = " + groupKey); boolean remove = removedAccessRights.containsKey(groupKey); boolean modify = modifiedAccessRights.containsKey(groupKey); boolean add = addedAccessRights.containsKey(groupKey); boolean overwrite = (modify || add); // Remove accessright if (remove) { // ("removing accessright, groupkey = " + groupKey); curAccessRight = XMLTool.removeChildFromParent(elExistingAccessRights, curAccessRight); } // Overwrite accessright else if (overwrite) { ExtendedMap params; if (modify) { params = modifiedAccessRights.get(groupKey); // ("modifying/overwriting accessright, groupkey = " + groupKey); } else // add == true: { params = addedAccessRights.get(groupKey); // ("adding/overwriting accessright, groupkey = " + groupKey); } Document docNewAccessRight = XMLTool.createDocument("foo"); Element elNewAccessRight = buildAccessRightElement( docNewAccessRight, docNewAccessRight.getDocumentElement(), groupKey, params); Element imported = (Element) docExistingAccessRights.importNode(elNewAccessRight, true); elExistingAccessRights.replaceChild(imported, curAccessRight); curAccessRight = imported; // Hvis vi overskriver eksisterende rettighet i stedet for å legge til, fordi den finnes fra // før // må vi fjerne rettigheten fra addedAccessRights, slik at vi ikke legger til den to ganger. if (add) { // ("Found an accessright that we wanted to add, that existed - we overwrite it // inseated, and removes the groupkey ("+groupKey+")from the addAccessRights hashtable so // that it // want be added later"); addedAccessRights.remove(groupKey); } // curAccessRight = (Element) curAccessRight.getNextSibling(); } else { curAccessRight = (Element) curAccessRight.getNextSibling(); } } // Add new accessrights for (Object addedAccessRightKey : addedAccessRights.keySet()) { String currentGroupKey = (String) addedAccessRightKey; // ("adding new accessright, groupkey = " + currentGroupKey); ExtendedMap params = addedAccessRights.get(currentGroupKey); Document docNewAccessRight = XMLTool.createDocument("foo"); Element elNewAccessRight = buildAccessRightElement( docNewAccessRight, docNewAccessRight.getDocumentElement(), currentGroupKey, params); elExistingAccessRights.appendChild( docExistingAccessRights.importNode(elNewAccessRight, true)); } return docExistingAccessRights; }
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; }
public Object getDataFromXML(Node node) { Document tmpDoc = XMLTool.createDocument(); tmpDoc.appendChild(tmpDoc.importNode(node, true)); return XMLTool.documentToString(tmpDoc); }
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); }
public Document toXML() { final Document doc = XMLTool.createDocument("sessions"); toXML(doc.getDocumentElement()); return doc; }