public String invoke( Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { try { Map<String, Object> groovyContext = FastMap.newInstance(); groovyContext.put("request", request); groovyContext.put("response", response); HttpSession session = request.getSession(); groovyContext.put("session", session); groovyContext.put("dispatcher", request.getAttribute("dispatcher")); groovyContext.put("delegator", request.getAttribute("delegator")); groovyContext.put("security", request.getAttribute("security")); groovyContext.put("locale", UtilHttp.getLocale(request)); groovyContext.put("timeZone", UtilHttp.getTimeZone(request)); groovyContext.put("userLogin", session.getAttribute("userLogin")); groovyContext.put( "parameters", UtilHttp.getCombinedMap( request, UtilMisc.toSet( "delegator", "dispatcher", "security", "locale", "timeZone", "userLogin"))); Object result = GroovyUtil.runScriptAtLocation(event.path + event.invoke, groovyContext); // check the result if (result != null && !(result instanceof String)) { throw new EventHandlerException( "Event did not return a String result, it returned a " + result.getClass().getName()); } return (String) result; } catch (Exception e) { throw new EventHandlerException("Groovy Event Error", e); } }
public static String replaceShoppingListItem( HttpServletRequest request, HttpServletResponse response) { String quantityStr = request.getParameter("quantity"); // just call the updateShoppingListItem service LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); Locale locale = UtilHttp.getLocale(request); BigDecimal quantity = null; try { quantity = new BigDecimal(quantityStr); } catch (Exception e) { // do nothing, just won't pass to service if it is null } Map<String, Object> serviceInMap = FastMap.newInstance(); serviceInMap.put("shoppingListId", request.getParameter("shoppingListId")); serviceInMap.put("shoppingListItemSeqId", request.getParameter("shoppingListItemSeqId")); serviceInMap.put("productId", request.getParameter("add_product_id")); serviceInMap.put("userLogin", userLogin); if (quantity != null) serviceInMap.put("quantity", quantity); Map<String, Object> result = null; try { result = dispatcher.runSync("updateShoppingListItem", serviceInMap); } catch (GenericServiceException e) { String errMsg = UtilProperties.getMessage( resource_error, "shoppingListEvents.error_calling_update", locale) + ": " + e.toString(); request.setAttribute("_ERROR_MESSAGE_", errMsg); String errorMsg = "Error calling the updateShoppingListItem in handleShoppingListItemVariant: " + e.toString(); Debug.logError(e, errorMsg, module); return "error"; } ServiceUtil.getMessages(request, result, "", "", "", "", "", "", ""); if ("error".equals(result.get(ModelService.RESPONSE_MESSAGE))) { return "error"; } else { return "success"; } }
public static String editShipmentPlan(HttpServletRequest request, HttpServletResponse response) throws GenericEntityException { LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); Locale locale = UtilHttp.getLocale(request); String picklistId; String orderId = request.getParameter("orderId"); List<GenericValue> pickListBin; GenericValue pickList; GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); pickListBin = delegator.findByAnd("PicklistBin", UtilMisc.toMap("primaryOrderId", orderId)); for (GenericValue gv : pickListBin) { picklistId = gv.getString("picklistId"); pickList = delegator.findByPrimaryKey("Picklist", UtilMisc.toMap("picklistId", picklistId)); String statusId = pickList.getString("statusId"); if ("PICKLIST_INPUT".equals(statusId)) { request.setAttribute( "_ERROR_MESSAGE_", "Order is already picked. Please pack through Auto Shipment"); return "error"; } } return "success"; }
public String invoke( Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { if (Debug.infoOn()) { Debug.logInfo("In GwtRpcGroovyEventHandler", module); } String requestPayload = null; try { requestPayload = GwtRpcPayloadUtil.getRequestPayload(request); } catch (IOException ioe) { throw new EventHandlerException("Exception while getting requestPayload", ioe); } catch (ServletException se) { throw new EventHandlerException("Exception while getting requestPayload", se); } HashMap<String, String> gwtParameters = GwtRpcPayloadUtil.getParameters(requestPayload); if (Debug.infoOn()) { Debug.logInfo("gwtParameters : " + gwtParameters, module); } if (null != gwtParameters) { HttpSession session = request.getSession(); Map<String, Object> groovyContext = FastMap.newInstance(); groovyContext.put("request", request); groovyContext.put("response", response); groovyContext.put("session", session); groovyContext.put("dispatcher", request.getAttribute("dispatcher")); groovyContext.put("delegator", request.getAttribute("delegator")); groovyContext.put("security", request.getAttribute("security")); groovyContext.put("locale", UtilHttp.getLocale(request)); groovyContext.put("timeZone", UtilHttp.getTimeZone(request)); groovyContext.put("userLogin", session.getAttribute("userLogin")); Map<String, Object> parameters = UtilHttp.getCombinedMap( request, UtilMisc.toSet( "delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")); if (Debug.infoOn()) { Debug.logInfo("parameters : " + parameters, module); } Set<String> keys = gwtParameters.keySet(); Iterator<String> iter = keys.iterator(); while (iter.hasNext()) { String key = iter.next(); parameters.put(key, gwtParameters.get(key)); } groovyContext.put("parameters", parameters); Object result = null; try { result = GroovyUtil.runScriptAtLocation(event.path + event.invoke, groovyContext); if (Debug.infoOn()) { Debug.logInfo("groovy script result : " + result, module); } } catch (GeneralException ge) { throw new EventHandlerException("Exception while executing groovy script : ", ge); } Map<String, Object> resultMap = (Map<String, Object>) result; // ServletContext sc = (ServletContext)request.getAttribute("servletContext"); // RequestDispatcher rd = sc.getRequestDispatcher("/gwtrpc"); request.setAttribute(GwtRpcPayload.OFBIZ_PAYLOAD, resultMap); request.setAttribute(GwtRpcPayload.REQUEST_PAYLOAD, requestPayload); /*try { rd.forward(request, response); } catch(IOException ioe) { throw new EventHandlerException("IO Exception while forwarding request to GWT RPC servlet : ",ioe); } catch(ServletException se) { throw new EventHandlerException("Servlet Exception while forwarding request to GWT RPC servlet : ",se); }*/ try { GwtRpcServletUtil servletUtil = new GwtRpcServletUtil(); String responsePayload = servletUtil.invokeServlet(request, response, requestPayload); servletUtil.writeResponse(servletContext, request, response, responsePayload); } catch (Exception e) { Debug.logError(e, "gwt remote servlet invocation failed " + e.getMessage(), module); throw new EventHandlerException(e); } } else { throw new EventHandlerException("GWT parameters are null : " + gwtParameters); } return "success"; }
/** * An HTTP WebEvent handler that checks to see is a userLogin is logged in. If not, the user is * forwarded to the login page. * * @param request The HTTP request object for the current JSP or Servlet request. * @param response The HTTP response object for the current JSP or Servlet request. * @return String */ public static String checkLogin(HttpServletRequest request, HttpServletResponse response) { GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); // anonymous shoppers are not logged in if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) { userLogin = null; } // user is logged in; check to see if they have globally logged out if not // check if they have permission for this login attempt; if not log them out if (userLogin != null) { Element rootElement = getRootElement(request); boolean hasLdapLoggedOut = false; if (rootElement != null) { String className = UtilXml.childElementValue( rootElement, "AuthenticationHandler", "org.ofbiz.ldap.openldap.OFBizLdapAuthenticationHandler"); try { Class<?> handlerClass = Class.forName(className); InterfaceOFBizAuthenticationHandler authenticationHandler = (InterfaceOFBizAuthenticationHandler) handlerClass.newInstance(); hasLdapLoggedOut = authenticationHandler.hasLdapLoggedOut(request, response, rootElement); } catch (ClassNotFoundException e) { Debug.logError(e, "Error calling checkLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (InstantiationException e) { Debug.logError(e, "Error calling checkLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (IllegalAccessException e) { Debug.logError(e, "Error calling checkLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (Exception e) { Debug.logError(e, "Error calling checkLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } } if (!hasBasePermission(userLogin, request) || isFlaggedLoggedOut(userLogin) || hasLdapLoggedOut) { Debug.logInfo("User does not have permission or is flagged as logged out", module); doBasicLogout(userLogin, request, response); userLogin = null; } } if (userLogin == null) { return login(request, response); } return "success"; }
protected static Element getRootElement(HttpServletRequest request) { if (Debug.infoOn()) { Debug.log("Applet config file: " + ldapConfig); } File configFile = new File(ldapConfig); FileInputStream configFileIS = null; Element rootElement = null; try { configFileIS = new FileInputStream(configFile); Document configDoc = UtilXml.readXmlDocument(configFileIS, "LDAP configuration file " + ldapConfig); rootElement = configDoc.getDocumentElement(); } catch (FileNotFoundException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (SAXException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (ParserConfigurationException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (IOException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } finally { if (configFileIS != null) { try { configFileIS.close(); } catch (IOException e) { } } } return rootElement; }
/** * An HTTP WebEvent handler that logs out a userLogin by clearing the session. * * @param request The HTTP request object for the current request. * @param response The HTTP response object for the current request. * @return Return a boolean which specifies whether or not the calling request should generate its * own content. This allows an event to override the default content. */ public static String logout(HttpServletRequest request, HttpServletResponse response) { // run the before-logout events RequestHandler rh = RequestHandler.getRequestHandler(request.getSession().getServletContext()); rh.runBeforeLogoutEvents(request, response); // invalidate the security group list cache GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); doBasicLogout(userLogin, request, response); Element rootElement = getRootElement(request); String result = "error"; if (rootElement != null) { String className = UtilXml.childElementValue( rootElement, "AuthenticationHandler", "org.ofbiz.ldap.openldap.OFBizLdapAuthenticationHandler"); try { Class<?> handlerClass = Class.forName(className); InterfaceOFBizAuthenticationHandler authenticationHandler = (InterfaceOFBizAuthenticationHandler) handlerClass.newInstance(); result = authenticationHandler.logout(request, response, rootElement); } catch (ClassNotFoundException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (InstantiationException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (IllegalAccessException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (Exception e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } } if (request.getAttribute("_AUTO_LOGIN_LOGOUT_") == null) { return autoLoginCheck(request, response); } return result; }
/** * An HTTP WebEvent handler that logs in a userLogin. This should run before the security check. * * @param request The HTTP request object for the current JSP or Servlet request. * @param response The HTTP response object for the current JSP or Servlet request. * @return Return a boolean which specifies whether or not the calling Servlet or JSP should * generate its own content. This allows an event to override the default content. */ public static String login(HttpServletRequest request, HttpServletResponse response) { Element rootElement = getRootElement(request); String result = "error"; if (rootElement != null) { String className = UtilXml.childElementValue( rootElement, "AuthenticationHandler", "org.ofbiz.ldap.openldap.OFBizLdapAuthenticationHandler"); try { Class<?> handlerClass = Class.forName(className); InterfaceOFBizAuthenticationHandler authenticationHandler = (InterfaceOFBizAuthenticationHandler) handlerClass.newInstance(); result = authenticationHandler.login(request, response, rootElement); } catch (ClassNotFoundException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (InstantiationException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (IllegalAccessException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (NamingException e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } catch (Exception e) { Debug.logError(e, "Error calling userLogin service", module); Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); String errMsg = UtilProperties.getMessage( resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); request.setAttribute("_ERROR_MESSAGE_", errMsg); } } if (result.equals("error")) { boolean useOFBizLoginWhenFail = Boolean.getBoolean( UtilXml.childElementValue(rootElement, "UseOFBizLoginWhenLDAPFail", "false")); if (useOFBizLoginWhenFail) { return LoginWorker.login(request, response); } } return result; }
public static String timeSheetChecker(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Delegator delegator = (Delegator) session.getAttribute("delegator"); GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); List<Map<String, Object>> noTimeEntryList = new LinkedList<Map<String, Object>>(); String partyId = userLogin.getString("partyId"); Timestamp now = UtilDateTime.nowTimestamp(); Timestamp weekStart = UtilDateTime.getWeekStart(now); if (UtilValidate.isEmpty(delegator)) { delegator = (Delegator) request.getAttribute("delegator"); } try { // should be scrum team or scrum master. EntityConditionList<EntityExpr> exprOrs = EntityCondition.makeCondition( UtilMisc.toList( EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, "SCRUM_TEAM"), EntityCondition.makeCondition( "roleTypeId", EntityOperator.EQUALS, "SCRUM_MASTER")), EntityOperator.OR); EntityConditionList<EntityCondition> exprAnds = EntityCondition.makeCondition( UtilMisc.toList( exprOrs, EntityCondition.makeCondition("partyId", EntityOperator.EQUALS, partyId)), EntityOperator.AND); List<GenericValue> partyRoleList = EntityQuery.use(delegator).from("PartyRole").where(exprAnds).queryList(); if (UtilValidate.isNotEmpty(partyRoleList)) { List<GenericValue> timesheetList = EntityQuery.use(delegator) .from("Timesheet") .where("partyId", partyId, "statusId", "TIMESHEET_IN_PROCESS") .cache(true) .queryList(); if (UtilValidate.isNotEmpty(timesheetList)) { for (GenericValue timesheetMap : timesheetList) { String timesheetId = timesheetMap.getString("timesheetId"); Timestamp timesheetDate = timesheetMap.getTimestamp("fromDate"); // check monday - friday for (int i = 0; i < 5; i++) { Timestamp realTimeDate = UtilDateTime.addDaysToTimestamp(timesheetDate, i); Timestamp nowStartDate = UtilDateTime.getDayStart(now); // compare week and compare date if ((timesheetDate.compareTo(weekStart) <= 0) && (realTimeDate.compareTo(nowStartDate) < 0)) { // check time entry List<GenericValue> timeEntryList = timesheetMap.getRelated( "TimeEntry", UtilMisc.toMap( "partyId", partyId, "timesheetId", timesheetId, "fromDate", realTimeDate), null, false); // check EmplLeave List<GenericValue> emplLeaveList = EntityQuery.use(delegator) .from("EmplLeave") .where("partyId", partyId, "fromDate", realTimeDate) .cache(true) .queryList(); if (UtilValidate.isEmpty(timeEntryList) && UtilValidate.isEmpty(emplLeaveList)) { Map<String, Object> noEntryMap = new HashMap<String, Object>(); noEntryMap.put("timesheetId", timesheetId); noTimeEntryList.add(noEntryMap); break; } } } } } } } catch (GenericEntityException EntEx) { EntEx.printStackTrace(); Debug.logError(EntEx.getMessage(), module); } if (UtilValidate.isNotEmpty(noTimeEntryList)) { StringBuilder warningDataBuffer = new StringBuilder(); int size = noTimeEntryList.size(); for (Map<String, Object> dataMap : noTimeEntryList) { if (--size == 0) { warningDataBuffer.append(dataMap.get("timesheetId")); } else { warningDataBuffer.append(dataMap.get("timesheetId")).append(", "); } warningDataBuffer.append(dataMap.get("timesheetId")); } String warningData = warningDataBuffer.toString(); Debug.logInfo("The following time sheet no time entry: [" + warningData + "]", module); request.setAttribute( "_ERROR_MESSAGE_", UtilProperties.getMessage( "scrumUiLabels", "ScrumTimesheetWarningMessage", UtilMisc.toMap("warningMessage", warningData), UtilHttp.getLocale(request))); } return "success"; }