protected String getProperty(Node node, String key) { NodeManager properties = node.getCloud().getNodeManager("properties"); Function get = properties.getFunction("get"); Parameters params = get.createParameters(); params.set("node", node); params.set("key", key); return (String) get.getFunctionValue(params); }
@Override public String getFunctionValue(final Node node, final Parameters parameters) { if (log.isDebugEnabled()) { log.debug("node #" + node.getNumber()); log.debug("params: " + parameters); } String status = getDownloadStatus(node); int timeout = 5; if (parameters.get(TIMEOUT) != null) { timeout = parameters.get(TIMEOUT); } if (status == null) { Action action = ActionRepository.getInstance().get("streams", "download_media"); if (action == null) { throw new IllegalStateException("Action could not be found"); } if (node.getCloud().may(action, null)) { synchronized (runningJobs) { Future<?> future = runningJobs.get(node.getNumber()); if (future == null) { setDownloadStatus(node, "busy: " + System.currentTimeMillis()); future = submit(node, parameters); ThreadPools.identify( future, DownloadFunction.class.getName() + " downloading... for #" + node.getNumber() + " - status: " + getDownloadStatus(node)); String fname = ThreadPools.getString(future); log.info("Future name: " + fname); try { status = (String) future.get(timeout, TimeUnit.SECONDS); log.info("status: " + status); } catch (TimeoutException te) { status = ThreadPools.getString(future); log.info("TimeoutException: " + status); } catch (Exception e) { log.error(e); } } else { status = ThreadPools.getString(future); } } log.info("status: " + status); return status; } else { throw new org.mmbase.security.SecurityException("Not allowed"); } } return status; }
protected void setProperty(Node node, String key, String value) { NodeManager properties = node.getCloud().getNodeManager("properties"); Function set = properties.getFunction("set"); Parameters params = set.createParameters(); params.set("node", node); params.set("key", key); if (value.length() > 255) { value = value.substring(0, 255); } params.set("value", value); set.getFunctionValue(params); }
protected MMObjectNode getGroupOrUserNode(Parameters a) { MMObjectNode groupOrUser = getNode(a.getString(PARAMETER_GROUPORUSER)); if (groupOrUser == null) throw new IllegalArgumentException( "There is no node with id '" + a.get(PARAMETER_GROUPORUSER) + "'"); MMObjectBuilder parent = groupOrUser.getBuilder(); MMObjectBuilder userBuilder = Authenticate.getInstance().getUserProvider().getUserBuilder(); if (!(parent instanceof Groups || userBuilder.getClass().isInstance(parent))) { throw new IllegalArgumentException( "Node '" + a.get(PARAMETER_GROUPORUSER) + "' does not represent a group or a user"); } return groupOrUser; }
@Override public void render(Parameters blockParameters, Writer w, RenderHints hints) throws FrameworkException { log.debug("Error rendering " + blockParameters); switch (getType()) { case BODY: try { decorateIntro(hints, w, "error"); w.write("<h1>" + error.status); w.write(": "); CharTransformer escape = new Xml(Xml.ESCAPE); w.write(escape.transform(error.exception.getMessage())); w.write(" "); w.write(escape.transform(url)); w.write("</h1>"); w.write("<pre>"); HttpServletRequest request = blockParameters.get(Parameter.REQUEST); error.getErrorReport(w, request, escape); w.write("</pre>"); decorateOutro(hints, w); } catch (IOException eio) { throw new FrameworkException(eio.getMessage(), eio); } break; default: } }
@SuppressWarnings("unchecked") protected void setBlockParametersForRender(State state, Parameters blockParameters) { ServletRequest request = state.getRequest(); String prefix = getPrefix(state); log.debug("prefix " + prefix); blockParameters.setAutoCasting(true); for (Map.Entry<String, String[]> entry : ((Map<String, String[]>) request.getParameterMap()).entrySet()) { String key = entry.getKey(); if (key.startsWith(prefix)) { log.trace("setting" + entry); blockParameters.setIfDefined(key.substring(prefix.length()), entry.getValue()); } } if (log.isDebugEnabled()) { log.debug("Set " + blockParameters); } }
@Override public String getProcessUrl( String path, Map<String, ?> parameters, Parameters frameworkParameters, boolean escapeAmps) throws FrameworkException { HttpServletRequest request = BasicUrlConverter.getUserRequest(frameworkParameters.get(Parameter.REQUEST)); State state = State.getState(request); frameworkParameters.set(ACTION, state.getId()); Url url = urlConverter.getProcessUrl(path, parameters, frameworkParameters, escapeAmps); if (url == Url.NOT) { log.debug("Fall back url"); return fallbackConverter .getProcessUrl(path, parameters, frameworkParameters, escapeAmps) .getUrl(); } else { log.debug("Url converter url " + url); return url.getUrl(); } }
/** * I think in the basic framework this method is never called explicitely, because processing is * done implicitly by the render */ @Override public void process( Processor processor, Parameters blockParameters, Parameters frameworkParameters) throws FrameworkException { HttpServletRequest request = frameworkParameters.get(Parameter.REQUEST); State state = State.getState(request); state.startBlock(frameworkParameters, null); setBlockParametersForProcess(state, blockParameters); processor.process(blockParameters); }
@Override public Block getRenderingBlock(Parameters frameworkParameters) { HttpServletRequest request = frameworkParameters.get(Parameter.REQUEST); State state = State.getState(request); if (state.isRendering()) { return state.getBlock(); } else { return null; } }
@SuppressWarnings("unchecked") @Override public <C> C setSettingValue(Setting<C> setting, Parameters parameters, C value) { if (parameters == null) throw new SecurityException("You should provide Cloud and request parameters"); boolean useSession = parameters.get(USE_REQ); if (useSession) { C ret = getSettingValue(setting, parameters); HttpServletRequest req = parameters.get(Parameter.REQUEST); req.getSession(true).setAttribute(getKey(setting), value); return ret; } else { Cloud cloud = parameters.get(Parameter.CLOUD); if (cloud.getUser().getRank() == org.mmbase.security.Rank.ADMIN) { saveSettingValue(setting, value); return (C) settingValues.put(setting, value); } else { throw new SecurityException("Permission denied"); } } }
@SuppressWarnings("unchecked") @Override public <C> C getSettingValue(Setting<C> setting, Parameters parameters) { boolean useSession = parameters != null && parameters.get(USE_REQ); if (useSession) { HttpServletRequest req = parameters.get(Parameter.REQUEST); if (req != null) { Object v = req.getSession(true).getAttribute(getKey(setting)); if (v != null) { return setting.getDataType().cast(v, null, null); } } } if (settingValues.containsKey(setting)) { return (C) settingValues.get(setting); } else { C settingValue = loadSettingValue(setting); if (settingValue != null) { settingValues.put(setting, settingValue); return settingValue; } return setting.getDataType().getDefaultValue(); } }
@Override public Node getUserNode(Parameters frameworkParameters) { Cloud cloud = frameworkParameters.get(Parameter.CLOUD); if (cloud == null) { return null; } else { try { int userNode = cloud.getCloudContext().getAuthentication().getNode(cloud.getUser()); if (cloud.hasNode(userNode)) { return cloud.getNode(userNode); } else { return null; } } catch (UnsupportedOperationException uoe) { // never mind return null; } } }
private Boolean sendMail(HttpServletRequest req, Node node, String email) { boolean send = false; Cloud cloud = node.getCloud(); String emailbuilder = "email"; try { Module sendmail = cloud.getCloudContext().getModule("sendmail"); emailbuilder = sendmail.getProperty("emailbuilder"); } catch (NotFoundException nfe) { log.warn("No email module " + nfe); } if (cloud.hasNodeManager(emailbuilder)) { NodeManager nm = cloud.getNodeManager(emailbuilder); Node message = nm.createNode(); String host = req.getHeader("host"); if (host == null || "".equals(host)) { try { host = java.net.InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException uhe) { log.warn("No host: " + uhe); } } String from = "downloader@" + host; // do a quick check if we've got something more or less valid Pattern p = Pattern.compile(".+@.+\\.[a-z]+"); Matcher m = p.matcher(from); if (!m.matches()) { from = "*****@*****.**"; } String mediaTitle = node.getStringValue("title"); String mediaUrl = getProperty(node, URL_KEY); StringBuilder body = new StringBuilder(); body.append("*This is an automated message / Dit is een geautomatiseerd bericht*"); body.append("\n\n*English*"); body.append("\n\nDear,"); body.append("\n\nWe have received your file belonging to media item titled '") .append(mediaTitle) .append("'. "); body.append("In about 1 hour, you can find your submission at: "); body.append("http://").append(host).append("/media/").append(node.getNumber()); body.append("\n\nKind regards,"); body.append("\n\n").append(host); body.append("\n\n\n*Nederlands*"); body.append("\n\nBeste,"); body.append("\n\nWe hebben je bestand voor het media item met de titel '") .append(mediaTitle) .append("' ontvangen. "); body.append("Je kunt je bijdrage hier over circa een uur terugvinden: "); body.append("http://").append(host).append("/media/").append(node.getNumber()); body.append("\n\nMet vriendelijke groet,"); body.append("\n\n").append(host); message.setValue("from", from); message.setValue("to", email); message.setValue("subject", "Download complete / Download voltooid"); message.setValue("body", body.toString()); message.commit(); Function mail = message.getFunction("mail"); Parameters mail_params = mail.createParameters(); mail_params.set("type", "oneshot"); mail.getFunctionValue(mail_params); if (log.isDebugEnabled()) { log.debug("Message download ready send to: " + email); } send = true; } else { log.warn("Can not send message - no emailbuilder installed."); } return send; }
/** * Basic Framework implicitely also processes, i'm not sure if we should require any framework to * do that (perhaps we could say, that the render method must process, if that is necessary, and * not yet done). */ @Override public void render( Renderer renderer, Parameters blockParameters, Parameters frameworkParameters, Writer w, WindowState windowState) throws FrameworkException { ServletRequest request = frameworkParameters.get(Parameter.REQUEST); if (request == null) { throw new IllegalArgumentException("No request object given"); } State state = State.getState(request); if (state .isRendering()) { // mm:component used during rending of a component, that's fine, but use a // new State. state = new State(request); log.debug("Alreadying rendering, taking a new state for sub-block-rendering: " + state); } log.debug("Rendering " + renderer.getBlock() + " " + renderer); Object prevHints = request.getAttribute(RenderHints.KEY); try { request.setAttribute(COMPONENT_CLASS_KEY, getComponentClass()); request.setAttribute(COMPONENT_CURRENTUSER_KEY, getUserNode(frameworkParameters)); Renderer actualRenderer = state.startBlock(frameworkParameters, renderer); if (!actualRenderer.equals(renderer)) { Parameters newBlockParameters = actualRenderer.getBlock().createParameters(); newBlockParameters.setAllIfDefined(blockParameters); blockParameters = newBlockParameters; } state.setAction(request.getParameter(ACTION.getName())); if (state.needsProcess()) { log.service("Performing action on " + actualRenderer.getBlock()); Processor processor = actualRenderer.getBlock().getProcessor(); state.process(processor); log.service("Processing " + actualRenderer.getBlock() + " " + processor); setBlockParametersForProcess(state, blockParameters); processor.process(blockParameters); state.endProcess(); } state.render(actualRenderer); setBlockParametersForRender(state, blockParameters); RenderHints hints = new RenderHints( actualRenderer, windowState, state.getId(), getComponentClass(), RenderHints.Mode.NORMAL); request.setAttribute(RenderHints.KEY, hints); actualRenderer.render(blockParameters, w, hints); request.setAttribute("org.mmbase.framework.hints", hints); } catch (FrameworkException fe) { log.debug(fe); URI uri = renderer.getUri(); Renderer error = new ErrorRenderer( renderer.getType(), renderer.getBlock(), (uri != null) ? uri.toString() : null, 500, fe); RenderHints hints = new RenderHints( error, windowState, state.getId(), getComponentClass(), RenderHints.Mode.NORMAL); error.render(blockParameters, w, hints); } finally { request.setAttribute(RenderHints.KEY, prevHints); state.endBlock(); } }
/** @todo */ protected void setBlockParametersForProcess(State state, Parameters blockParameters) { ServletRequest request = state.getRequest(); for (Map.Entry<String, ?> entry : blockParameters.toMap().entrySet()) { request.setAttribute(entry.getKey(), entry.getValue()); } }