Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* This file is part of *** M y C o R e ***
* See http://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see .
*/
package org.mycore.common.xsl;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Transformer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mycore.common.MCRConstants;
import org.mycore.common.MCRSession;
import org.mycore.common.MCRSessionMgr;
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.frontend.MCRFrontendUtil;
import org.mycore.frontend.servlets.MCRServlet;
import org.mycore.frontend.servlets.MCRServletJob;
/**
* Collects parameters used in XSL transformations, by copying them from
* MCRConfiguration, from the HTTP and MyCoRe session, from request attributes etc.
*
* @author Frank L\u00FCtzenkirchen
*/
public class MCRParameterCollector {
private static final Logger LOGGER = LogManager.getLogger(MCRParameterCollector.class);
/** The collected parameters */
private Map parameters = new HashMap<>();
/** If true (which is default), only those parameters starting with "XSL." are copied from session and request */
private boolean onlySetXSLParameters = true;
private boolean modified = true;
private int hashCode;
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
* First, all configuration properties from MCRConfiguration are copied.
* Second, those variables stored in the HTTP session, that start with "XSL." are copied.
* Next, variables stored in the MCRSession are copied.
* Next, HTTP request parameters are copied.
*
* Only those parameters starting with "XSL." are copied from session and request,
*
* @param request the HttpRequest causing the XSL transformation, must NOT be null
*/
public MCRParameterCollector(HttpServletRequest request) {
this(request, true);
}
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
* First, all configuration properties from MCRConfiguration are copied.
* Second, those variables stored in the HTTP session, that start with "XSL." are copied.
* Next, variables stored in the MCRSession are copied.
* Next, HTTP request parameters are copied.
* Next, HTTP request attributes are copied.
*
* @param request the HttpRequest causing the XSL transformation, must NOT be null
* @param onlySetXSLParameters if true, only those parameters starting with "XSL."
* are copied from session and request
*/
public MCRParameterCollector(HttpServletRequest request, boolean onlySetXSLParameters) {
this.onlySetXSLParameters = onlySetXSLParameters;
setFromConfiguration();
HttpSession session = request.getSession(false);
if (session != null) {
setFromSession(session);
}
if (!MCRSessionMgr.isLocked()) {
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
setFromSession(mcrSession);
setUnmodifyableParameters(mcrSession, request);
}
setFromRequestParameters(request);
setFromRequestAttributes(request);
setFromRequestHeader(request);
if (session != null) {
setSessionID(session, request.isRequestedSessionIdFromCookie());
}
debugSessionParameters();
}
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
* First, all configuration properties from MCRConfiguration are copied.
* Next, those variables stored in the MCRSession that start with "XSL." are copied.
*/
public MCRParameterCollector() {
this(true);
}
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
* First, all configuration properties from MCRConfiguration are copied.
* Next, those variables stored in the MCRSession are copied.
*
* @param onlySetXSLParameters if true, only those parameters starting with "XSL." are copied from session
*/
public MCRParameterCollector(boolean onlySetXSLParameters) {
this.onlySetXSLParameters = onlySetXSLParameters;
setFromConfiguration();
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
setFromSession(mcrSession);
setUnmodifyableParameters(mcrSession, null);
debugSessionParameters();
}
/**
* Sets the parameter with the given name
*/
public void setParameter(String name, Object value) {
parameters.put(name, value);
modified = true;
}
/**
* Sets all parameters from the given map
*/
public void setParameters(Map param) {
parameters.putAll(param);
modified = true;
}
/**
* Sets the parameter only if it is not empty and starts with "XSL." or onlySetXSLParameters is false
*/
private void setXSLParameter(String name, String value) {
if ((value == null) || value.isEmpty()) {
return;
}
if (name.startsWith("XSL.")) {
parameters.put(name.substring(4), value);
} else if (!onlySetXSLParameters) {
parameters.put(name, value);
}
}
/**
* Returns the parameter with the given name
*/
public String getParameter(String name, String defaultValue) {
Object val = parameters.get(name);
return (val == null) ? defaultValue : val.toString();
}
/**
* Returns the parameter map.
*/
public Map getParameterMap() {
return Collections.unmodifiableMap(parameters);
}
/**
* Copies all MCRConfiguration properties as XSL parameters.
*/
private void setFromConfiguration() {
for (Map.Entry property : MCRConfiguration2.getPropertiesMap().entrySet()) {
parameters.put(property.getKey(), property.getValue());
}
}
/**
* Sets those session variables as XSL parameters that start with "XSL.",
* others will be ignored. The "XSL." prefix is cut off from the name.
*/
private void setFromSession(HttpSession session) {
for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
String name = e.nextElement();
setXSLParameter(name, session.getAttribute(name).toString());
}
}
/**
* Sets those session variables as XSL parameters that start with "XSL.",
* others will be ignored. The "XSL." prefix is cut off from the name.
*/
private void setFromSession(MCRSession session) {
Objects.requireNonNull(session, "Session needs to be not null!");
for (Map.Entry