use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.
the class OfbizUrlTransform method getWriter.
@Override
@SuppressWarnings("rawtypes")
public Writer getWriter(final Writer out, Map args) {
final StringBuilder buf = new StringBuilder();
final boolean fullPath = checkBooleanArg(args, "fullPath", false);
final boolean secure = checkBooleanArg(args, "secure", false);
final boolean encode = checkBooleanArg(args, "encode", true);
final String webSiteId = convertToString(args.get("webSiteId"));
return new Writer(out) {
@Override
public void close() throws IOException {
try {
Environment env = Environment.getCurrentEnvironment();
// Handle prefix.
String prefixString = convertToString(env.getVariable("urlPrefix"));
if (!prefixString.isEmpty()) {
String bufString = buf.toString();
boolean prefixSlash = prefixString.endsWith("/");
boolean bufSlash = bufString.startsWith("/");
if (prefixSlash && bufSlash) {
bufString = bufString.substring(1);
} else if (!prefixSlash && !bufSlash) {
bufString = "/" + bufString;
}
out.write(prefixString + bufString);
return;
}
HttpServletRequest request = FreeMarkerWorker.unwrap(env.getVariable("request"));
// Handle web site ID.
if (!webSiteId.isEmpty()) {
Delegator delegator = FreeMarkerWorker.unwrap(env.getVariable("delegator"));
if (request != null && delegator == null) {
delegator = (Delegator) request.getAttribute("delegator");
}
if (delegator == null) {
throw new IllegalStateException("Delegator not found");
}
WebappInfo webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
StringBuilder newUrlBuff = new StringBuilder(250);
OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
builder.buildFullUrl(newUrlBuff, buf.toString(), secure);
String newUrl = newUrlBuff.toString();
if (encode) {
newUrl = URLEncoder.encode(newUrl, "UTF-8");
}
out.write(newUrl);
return;
}
if (request != null) {
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
HttpServletResponse response = FreeMarkerWorker.unwrap(env.getVariable("response"));
String requestUrl = buf.toString();
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
out.write(rh.makeLink(request, response, requestUrl, fullPath, secure, encode));
} else {
out.write(buf.toString());
}
} catch (Exception e) {
Debug.logWarning(e, "Exception thrown while running ofbizUrl transform", module);
throw new IOException(e);
}
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void write(char[] cbuf, int off, int len) {
buf.append(cbuf, off, len);
}
};
}
use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.
the class IterateSectionWidget method renderNextPrev.
public void renderNextPrev(Appendable writer, Map<String, Object> context, int listSize, int actualPageSize) throws IOException {
String targetService = this.getPaginateTarget(context);
if (targetService == null) {
targetService = "${targetService}";
}
Map<String, Object> inputFields = UtilGenerics.checkMap(context.get("requestParameters"));
Map<String, Object> queryStringMap = UtilGenerics.toMap(context.get("queryStringMap"));
if (UtilValidate.isNotEmpty(queryStringMap)) {
inputFields.putAll(queryStringMap);
}
String queryString = UtilHttp.urlEncodeArgs(inputFields);
int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
queryString = UtilHttp.stripViewParamsFromQueryString(queryString, "" + paginatorNumber);
if (UtilValidate.isEmpty(targetService)) {
Debug.logWarning("TargetService is empty.", module);
return;
}
int viewIndex = -1;
try {
viewIndex = ((Integer) context.get("viewIndex")).intValue();
} catch (Exception e) {
viewIndex = 0;
}
int viewSize = -1;
try {
viewSize = ((Integer) context.get("viewSize")).intValue();
} catch (Exception e) {
viewSize = this.getViewSize();
}
int lowIndex = viewIndex * viewSize;
int highIndex = (viewIndex + 1) * viewSize;
// if this is all there seems to be (if listSize < 0, then size is unknown)
if (actualPageSize >= listSize && listSize > 0) {
return;
}
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
writer.append("<table border=\"0\" width=\"100%\" cellpadding=\"2\">\n");
writer.append(" <tr>\n");
writer.append(" <td align=\"right\">\n");
writer.append(" <b>\n");
if (viewIndex > 0) {
writer.append(" <a href=\"");
StringBuilder linkText = new StringBuilder(targetService);
if (linkText.indexOf("?") < 0) {
linkText.append("?");
} else {
linkText.append("&");
}
if (UtilValidate.isNotEmpty(queryString)) {
linkText.append(queryString).append("&");
}
linkText.append("VIEW_SIZE_" + paginatorNumber + "=").append(viewSize).append("&VIEW_INDEX_" + paginatorNumber + "=").append(viewIndex - 1).append("\"");
// make the link
writer.append(rh.makeLink(request, response, linkText.toString(), false, false, false));
String previous = UtilProperties.getMessage("CommonUiLabels", "CommonPrevious", (Locale) context.get("locale"));
writer.append(" class=\"buttontext\">[").append(previous).append("]</a>\n");
}
if (listSize > 0) {
Map<String, Integer> messageMap = UtilMisc.toMap("lowCount", Integer.valueOf(lowIndex + 1), "highCount", Integer.valueOf(lowIndex + actualPageSize), "total", Integer.valueOf(listSize));
String commonDisplaying = UtilProperties.getMessage("CommonUiLabels", "CommonDisplaying", messageMap, (Locale) context.get("locale"));
writer.append(" <span class=\"tabletext\">").append(commonDisplaying).append("</span> \n");
}
if (highIndex < listSize) {
writer.append(" <a href=\"");
StringBuilder linkText = new StringBuilder(targetService);
if (linkText.indexOf("?") < 0) {
linkText.append("?");
} else {
linkText.append("&");
}
if (UtilValidate.isNotEmpty(queryString)) {
linkText.append(queryString).append("&");
}
linkText.append("VIEW_SIZE_" + paginatorNumber + "=").append(viewSize).append("&VIEW_INDEX_" + paginatorNumber + "=").append(viewIndex + 1).append("\"");
// make the link
writer.append(rh.makeLink(request, response, linkText.toString(), false, false, false));
String next = UtilProperties.getMessage("CommonUiLabels", "CommonNext", (Locale) context.get("locale"));
writer.append(" class=\"buttontext\">[").append(next).append("]</a>\n");
}
writer.append(" </b>\n");
writer.append(" </td>\n");
writer.append(" </tr>\n");
writer.append("</table>\n");
}
use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.
the class WidgetWorker method determineAutoLinkType.
public static String determineAutoLinkType(String linkType, String target, String targetType, HttpServletRequest request) {
if ("auto".equals(linkType)) {
if ("intra-app".equals(targetType)) {
String requestUri = (target.indexOf('?') > -1) ? target.substring(0, target.indexOf('?')) : target;
ServletContext servletContext = request.getSession().getServletContext();
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
ConfigXMLReader.RequestMap requestMap = null;
try {
requestMap = rh.getControllerConfig().getRequestMapMap().get(requestUri);
} catch (WebAppConfigurationException e) {
Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
}
if (requestMap != null && requestMap.event != null) {
return "hidden-form";
}
}
return "anchor";
}
return linkType;
}
use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.
the class RenderSubContentCacheTransform method getWriter.
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final Environment env = Environment.getCurrentEnvironment();
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
final Map<String, Object> templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
FreeMarkerWorker.getSiteParameters(request, templateRoot);
final Map<String, Object> savedValuesUp = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
FreeMarkerWorker.overrideWithArgs(templateRoot, args);
final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
List<Map<String, ? extends Object>> trail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String contentAssocPredicateId = (String) templateRoot.get("contentAssocPredicateId");
String strNullThruDatesOnly = (String) templateRoot.get("nullThruDatesOnly");
Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
String thisSubContentId = (String) templateRoot.get("subContentId");
final boolean directAssocMode = UtilValidate.isNotEmpty(thisSubContentId) ? true : false;
GenericValue val = null;
try {
val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
} catch (GeneralException e) {
throw new RuntimeException("Error getting current content. " + e.toString());
}
final GenericValue view = val;
String dataResourceId = null;
String subContentIdSub = null;
if (view != null) {
try {
dataResourceId = (String) view.get("drDataResourceId");
} catch (IllegalArgumentException e) {
dataResourceId = (String) view.get("dataResourceId");
}
subContentIdSub = (String) view.get("contentId");
}
// This order is taken so that the dataResourceType can be overridden in the transform arguments.
String subDataResourceTypeId = (String) templateRoot.get("subDataResourceTypeId");
if (UtilValidate.isEmpty(subDataResourceTypeId) && view != null) {
try {
subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
} catch (IllegalArgumentException e) {
// view may be "Content"
}
// TODO: If this value is still empty then it is probably necessary to get a value from
// the parent context. But it will already have one and it is the same context that is
// being passed.
}
String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateRoot);
templateRoot.put("drDataResourceId", dataResourceId);
templateRoot.put("mimeTypeId", mimeTypeId);
templateRoot.put("dataResourceId", dataResourceId);
templateRoot.put("subContentId", subContentIdSub);
templateRoot.put("subDataResourceTypeId", subDataResourceTypeId);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
try {
renderSubContent();
FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
public void renderSubContent() throws IOException {
List<Map<String, ? extends Object>> passedGlobalNodeTrail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String editRequestName = (String) templateRoot.get("editRequestName");
GenericValue thisView = null;
if (view != null) {
thisView = view;
} else if (passedGlobalNodeTrail.size() > 0) {
Map<String, ? extends Object> map = UtilGenerics.checkMap(passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1));
if (Debug.infoOn()) {
Debug.logInfo("in Render(3), map ." + map, module);
}
if (map != null) {
thisView = (GenericValue) map.get("value");
}
}
String mimeTypeId = (String) templateRoot.get("mimeTypeId");
Locale locale = (Locale) templateRoot.get("locale");
if (locale == null)
locale = Locale.getDefault();
if (UtilValidate.isNotEmpty(editRequestName)) {
String editStyle = getEditStyle();
openEditWrap(out, editStyle);
}
if (thisView != null) {
String contentId = thisView.getString("contentId");
if (contentId != null) {
try {
ContentWorker.renderContentAsText(dispatcher, contentId, out, templateRoot, locale, mimeTypeId, null, null, true);
} catch (GeneralException e) {
Debug.logError(e, "Error rendering content", module);
throw new IOException("Error rendering thisView:" + thisView + " msg:" + e.toString());
}
}
}
if (UtilValidate.isNotEmpty(editRequestName)) {
closeEditWrap(out, editRequestName);
}
}
public void openEditWrap(Writer out, String editStyle) throws IOException {
String divStr = "<div class=\"" + editStyle + "\">";
out.write(divStr);
}
public void closeEditWrap(Writer out, String editRequestName) throws IOException {
String fullRequest = editRequestName;
String contentId = null;
String contentIdTo = null;
String contentAssocTypeId = null;
String mapKey = null;
String fromDate = null;
if (!directAssocMode) {
contentIdTo = (String) templateRoot.get("contentId");
contentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
mapKey = (String) templateRoot.get("mapKey");
fromDate = (String) templateRoot.get("fromDate");
if (Debug.infoOn())
Debug.logInfo("in Render(0), view ." + view, module);
if (view != null) {
ModelEntity modelEntity = view.getModelEntity();
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null)
contentId = view.getString("caContentId");
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null)
contentId = view.getString("contentId");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null)
contentIdTo = view.getString("caContentIdTo");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null)
contentIdTo = view.getString("contentIdTo");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null)
contentAssocTypeId = view.getString("caContentAssocTypeId");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null)
contentAssocTypeId = view.getString("contentAssocTypeId");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null)
mapKey = view.getString("caMapKey");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null)
mapKey = view.getString("mapKey");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null)
fromDate = view.getString("caFromDate");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null)
fromDate = view.getString("fromDate");
}
} else {
contentId = (String) templateRoot.get("subContentId");
}
if (Debug.infoOn())
Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo, module);
String delim = "?";
if (UtilValidate.isNotEmpty(contentId)) {
fullRequest += delim + "contentId=" + contentId;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentIdTo)) {
fullRequest += delim + "contentIdTo=" + contentIdTo;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId;
delim = "&";
}
if (UtilValidate.isNotEmpty(mapKey)) {
fullRequest += delim + "mapKey=" + mapKey;
delim = "&";
}
if (UtilValidate.isNotEmpty(fromDate)) {
fullRequest += delim + "fromDate=" + fromDate;
delim = "&";
}
if (Debug.infoOn())
Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo, module);
out.write("<a href=\"");
ServletContext servletContext = request.getSession().getServletContext();
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
out.append(rh.makeLink(request, response, "/" + fullRequest, false, false, true));
out.write("\">Edit</a>");
out.write("</div>");
}
public String getEditStyle() {
String editStyle = (String) templateRoot.get("editStyle");
if (UtilValidate.isEmpty(editStyle)) {
editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
}
if (UtilValidate.isEmpty(editStyle)) {
editStyle = "buttontext";
}
return editStyle;
}
};
}
use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.
the class UrlRegexpTransform method getWriter.
public Writer getWriter(final Writer out, Map args) {
final StringBuffer buf = new StringBuffer();
final boolean fullPath = checkArg(args, "fullPath", false);
final boolean secure = checkArg(args, "secure", false);
final boolean encode = checkArg(args, "encode", true);
return new Writer(out) {
public void write(char[] cbuf, int off, int len) {
buf.append(cbuf, off, len);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
try {
Environment env = Environment.getCurrentEnvironment();
BeanModel req = (BeanModel) env.getVariable("request");
BeanModel res = (BeanModel) env.getVariable("response");
Object prefix = env.getVariable("urlPrefix");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
HttpServletResponse response = null;
if (res != null) {
response = (HttpServletResponse) res.getWrappedObject();
}
HttpSession session = request.getSession();
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
// anonymous shoppers are not logged in
if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) {
userLogin = null;
}
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure || request.isSecure(), encode), userLogin == null));
} else if (prefix != null) {
if (prefix instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) prefix;
String prefixString = s.getAsString();
String bufString = buf.toString();
boolean prefixSlash = prefixString.endsWith("/");
boolean bufSlash = bufString.startsWith("/");
if (prefixSlash && bufSlash) {
bufString = bufString.substring(1);
} else if (!prefixSlash && !bufSlash) {
bufString = "/" + bufString;
}
out.write(prefixString + bufString);
}
} else {
out.write(buf.toString());
}
} catch (IOException | TemplateModelException e) {
throw new IOException(e.getMessage());
}
}
};
}
Aggregations