/** * Do we need to show the upgrade wizard prompt? */ public boolean isDue() { if (isUpToDate) return false; // only admin users should see this if (!Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) return false; // only show when Jenkins is fully up & running WebApp wa = WebApp.getCurrent(); if (wa==null || !(wa.getApp() instanceof Jenkins)) return false; return System.currentTimeMillis() > SetupWizard.getUpdateStateFile().lastModified(); }
Stapler stapler = Stapler.getCurrent(); if (stapler != null) { try { WebApp webapp = WebApp.getCurrent(); MetaClass meta = webapp.getMetaClass(this); Script s = meta.loadTearOff(JellyClassTearOff.class).findScript("newInstanceDetail"); if (s == null) { return ""; StringWriter sw = new StringWriter(); XMLOutput xml = dsi.createXMLOutput(sw, true); dsi.invokeScript(Stapler.getCurrentRequest(), Stapler.getCurrentResponse(), s, this, xml); return sw.toString(); } catch (Exception e) {
public void handle(ServletRequest request, ServletResponse response, AccessDeniedException cause) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse rsp = (HttpServletResponse) response; rsp.setStatus(HttpServletResponse.SC_FORBIDDEN); req.setAttribute("exception",cause); if (cause instanceof AccessDeniedException2) { ((AccessDeniedException2)cause).reportAsHeaders(rsp); } WebApp.get(Jenkins.getInstance().servletContext).getSomeStapler() .invoke(req,rsp, Jenkins.getInstance(), "/accessDenied"); } }
private void resetMetaClassCache() { // to allow the change to be effective, i.e. rebuild the MetaClass using the new whitelist WebApp.get(Jenkins.get().servletContext).clearMetaClassCache(); }
/** * Sets up Stapler to use our crumb issuer. */ @Initializer public static void initStaplerCrumbIssuer() { WebApp.get(Jenkins.getInstance().servletContext).setCrumbIssuer(new org.kohsuke.stapler.CrumbIssuer() { @Override public String issueCrumb(StaplerRequest request) { CrumbIssuer ci = Jenkins.getInstance().getCrumbIssuer(); return ci!=null ? ci.getCrumb(request) : DEFAULT.issueCrumb(request); } @Override public void validateCrumb(StaplerRequest request, String submittedCrumb) { CrumbIssuer ci = Jenkins.getInstance().getCrumbIssuer(); if (ci==null) { DEFAULT.validateCrumb(request,submittedCrumb); } else { if (!ci.validateCrumb(request, ci.getDescriptor().getCrumbSalt(), submittedCrumb)) throw new SecurityException("Crumb didn't match"); } } }); }
protected List<String> getPossibleViewNames(String baseName) { List<String> names = new ArrayList<String>(); for (Facet f : WebApp.get(Jenkins.getInstance().servletContext).facets) { if (f instanceof JellyCompatibleFacet) { JellyCompatibleFacet jcf = (JellyCompatibleFacet) f; for (String ext : jcf.getScriptExtensions()) names.add(baseName +ext); } } return names; }
@Override protected int run() throws Exception { Jenkins j = Jenkins.getInstance(); // Or perhaps simpler to inline the thread body of doReload? j.doReload(); Object app; while ((app = WebApp.get(j.servletContext).getApp()) instanceof HudsonIsLoading) { Thread.sleep(100); } if (app instanceof Jenkins) { return 0; } else if (app instanceof JenkinsReloadFailed) { Throwable t = ((JenkinsReloadFailed) app).cause; if (t instanceof Exception) { throw (Exception) t; } else { throw new RuntimeException(t); } } else { stderr.println("Unexpected status " + app); return 1; // could throw JenkinsReloadFailed.cause if it were not deprecated } }
@Override public Step newInstance(StaplerRequest req, JSONObject formData) throws FormException { if (req == null) { // should not happen return super.newInstance(null, formData); } // A modified version of RequestImpl.TypePair.convertJSON. // Works around the fact that Stapler does not call back into Descriptor.newInstance for nested objects (JENKINS-31458); // and propertiesMap virtual field name; and null values for unselected properties. List<JobProperty> properties = new ArrayList<JobProperty>(); ClassLoader cl = req.getStapler().getWebApp().getClassLoader(); @SuppressWarnings("unchecked") Set<Map.Entry<String,Object>> entrySet = formData.getJSONObject("propertiesMap").entrySet(); for (Map.Entry<String,Object> e : entrySet) { if (e.getValue() instanceof JSONObject) { String className = e.getKey().replace('-', '.'); // decode JSON-safe class name escaping Class<? extends JobProperty> itemType; try { itemType = cl.loadClass(className).asSubclass(JobProperty.class); } catch (ClassNotFoundException x) { throw new FormException(x, "propertiesMap"); } JobPropertyDescriptor d = (JobPropertyDescriptor) Jenkins.getActiveInstance().getDescriptorOrDie(itemType); JSONObject more = (JSONObject) e.getValue(); JobProperty property = d.newInstance(req, more); if (property != null) { properties.add(property); } } } return new JobPropertyStep(properties); }
/** * Returns true if this descriptor has <tt>config.jelly</tt>. */ public final boolean hasConfigScreen() { MetaClass c = WebApp.getCurrent().getMetaClass(getClass()); try { JellyClassTearOff tearOff = c.loadTearOff(JellyClassTearOff.class); return tearOff.findScript(getConfigPage())!=null; } catch(JellyException e) { return false; } }
/** * Reloads the configuration. */ @RequirePOST public synchronized HttpResponse doReload() throws IOException { checkPermission(ADMINISTER); LOGGER.log(Level.WARNING, "Reloading Jenkins as requested by {0}", getAuthentication().getName()); // engage "loading ..." UI and then run the actual task in a separate thread WebApp.get(servletContext).setApp(new HudsonIsLoading()); new Thread("Jenkins config reload thread") { @Override public void run() { try { ACL.impersonate(ACL.SYSTEM); reload(); } catch (Exception e) { LOGGER.log(SEVERE,"Failed to reload Jenkins config",e); new JenkinsReloadFailed(e).publish(servletContext,root); } } }.start(); return HttpResponses.redirectViaContextPath("/"); }
/** * Evaluates the Jelly script submitted by the client. * * This is useful for system administration as well as unit testing. */ @RequirePOST public void doEval(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { checkPermission(RUN_SCRIPTS); try { MetaClass mc = WebApp.getCurrent().getMetaClass(getClass()); Script script = mc.classLoader.loadTearOff(JellyClassLoaderTearOff.class).createContext().compileScript(new InputSource(req.getReader())); new JellyRequestDispatcher(this,script).forward(req,rsp); } catch (JellyException e) { throw new ServletException(e); } }
this.root = root; this.servletContext = context; computeVersion(context); if(theInstance!=null) throw new IllegalStateException("second instance"); TextFile secretFile = new TextFile(new File(getRootDir(),"secret.key")); if(secretFile.exists()) { secretKey = secretFile.readTrim(); pluginManager = PluginManager.createDefault(this); this.pluginManager = pluginManager; WebApp webApp = WebApp.get(servletContext); webApp.setClassLoader(pluginManager.uberClassLoader); webApp.setJsonInErrorMessageSanitizer(RedactSecretJsonInErrorMessageSanitizer.INSTANCE); webApp.setFilterForGetMethods(typedFilter); webApp.setFilterForFields(typedFilter); webApp.setFilterForDoActions(new DoActionFilter()); webApp.setFilteredGetterTriggerListener(actionListener); webApp.setFilteredDoActionTriggerListener(actionListener); webApp.setFilteredFieldTriggerListener(actionListener); executeReactor( is, loadTasks(), // load jobs
public JellyRequestDispatcher(Object it, Script script) { this.it = it; this.script = script; facet = WebApp.getCurrent().getFacet(JellyFacet.class); }
this.root = root; this.servletContext = context; computeVersion(context); if(theInstance!=null) throw new IllegalStateException("second instance"); TextFile secretFile = new TextFile(new File(getRootDir(),"secret.key")); if(secretFile.exists()) { secretKey = secretFile.readTrim(); this.pluginManager = pluginManager; WebApp.get(servletContext).setClassLoader(pluginManager.uberClassLoader); executeReactor( is, loadTasks(), // load jobs
public ContextMenu from(ModelObjectWithContextMenu self, StaplerRequest request, StaplerResponse response, String view) throws JellyException, IOException { WebApp webApp = WebApp.getCurrent(); final Script s = webApp.getMetaClass(self).getTearOff(JellyClassTearOff.class).findScript(view); if (s!=null) { JellyFacet facet = webApp.getFacet(JellyFacet.class); request.setAttribute("taskTags",this); // <l:task> will look for this variable and populate us request.setAttribute("mode","side-panel"); // run sidepanel but ignore generated HTML facet.scriptInvoker.invokeScript(request,response,new Script() { public Script compile() throws JellyException { return this; } public void run(JellyContext context, XMLOutput output) throws JellyTagException { Functions.initPageVariables(context); s.run(context,output); } },self,new XMLOutput(new DefaultHandler())); } else if (self instanceof Actionable) { // fallback this.addAll(((Actionable)self).getAllActions()); } return this; } }
public void write(StaplerRequest req, XMLOutput out) throws SAXException, IOException { if(inclusionFragment!=null) { out.write(inclusionFragment); return; } if (script!=null) try { WebApp.getCurrent().getFacet(JellyFacet.class).scriptInvoker.invokeScript(req, Stapler.getCurrentResponse(), script, this, out); } catch (JellyTagException e) { throw new IOException("Failed to execute Jelly script for adjunct "+name,e); } if(hasCss) out.write("<link rel='stylesheet' href='" + getBaseName(req)+".css' type='text/css' />"); if(hasJavaScript) out.write("<script src='" + getBaseName(req) +".js' type='text/javascript'></script>"); }
@Override protected Script resolveDefinition(JellyContext context) throws JellyTagException { Object it = expr.evaluate(context); if (it==null) throw new JellyTagException("'"+ expr.getExpressionText() +"' evaluated to null"); try { WebApp webApp = WebApp.getCurrent(); MetaClass c = webApp.getMetaClass(it instanceof Class ? Klass.java((Class)it): webApp.getKlass(it)); // prefer 'foo.jellytag' to avoid tags from showing up as views, // but for backward compatibility, support the plain .jelly extension as well. Script tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jellytag"); if (tag==null) tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jelly"); if (tag ==null) throw new JellyTagException("No such tag file "+tagName+".jellytag in "+c); return tag; } catch (JellyException e) { throw new JellyTagException("Failed to load "+tagName+".jellytag from "+it,e); } } };
public RequestDispatcher createRequestDispatcher(RequestImpl request, Klass type, Object it, String viewName) throws IOException { MetaClass mc = request.stapler.getWebApp().getMetaClass(type); return createDispatcher(it, viewName, mc); }
/** * Sets the classloader used by {@link StaplerRequest#bindJSON(Class, JSONObject)} and its sibling methods. * * @deprecated * Use {@link WebApp#setClassLoader(ClassLoader)} */ public static void setClassLoader( ServletContext context, ClassLoader classLoader ) { WebApp.get(context).setClassLoader(classLoader); }
/*package*/ MetaClass(WebApp webApp, Class clazz) { this.clazz = clazz; this.webApp = webApp; this.baseClass = webApp.getMetaClass(clazz.getSuperclass()); this.classLoader = MetaClassLoader.get(clazz.getClassLoader()); buildDispatchers( new ClassDescriptor(clazz,null/*support wrappers*/)); }