@RequirePOST
    public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, ServletException {
        try {
            Jenkins.getInstance().checkPermission(UPLOAD_PLUGINS);

            ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());

            // Parse the request
            FileItem fileItem = upload.parseRequest(req).get(0);
            String fileName = Util.getFileName(fileItem.getName());
            if("".equals(fileName)){
                return new HttpRedirect("advanced");
            }
            // we allow the upload of the new jpi's and the legacy hpi's
            if(!fileName.endsWith(".jpi") && !fileName.endsWith(".hpi")){
                throw new Failure(hudson.model.Messages.Hudson_NotAPlugin(fileName));
            }

            // first copy into a temporary file name
            File t = File.createTempFile("uploaded", ".jpi");
            t.deleteOnExit();
            try {
                fileItem.write(t);
            } catch (Exception e) {
                // Exception thrown is too generic so at least limit the scope where it can occur
                throw new ServletException(e);
            }
            fileItem.delete();

            final String baseName = identifyPluginShortName(t);

            pluginUploaded = true;

            JSONArray dependencies = new JSONArray();
            try {
                Manifest m = new JarFile(t).getManifest();
                String deps = m.getMainAttributes().getValue("Plugin-Dependencies");

                if (StringUtils.isNotBlank(deps)) {
                    // now we get to parse it!
                    String[] plugins = deps.split(",");
                    for (String p : plugins) {
                        // should have name:version[;resolution:=optional]
                        String[] attrs = p.split("[:;]");
                        dependencies.add(new JSONObject()
                                .element("name", attrs[0])
                                .element("version", attrs[1])
                                .element("optional", p.contains("resolution:=optional")));
                    }
                }
            } catch(IOException e) {
                LOGGER.log(WARNING, "Unable to setup dependency list for plugin upload", e);
            }

            // Now create a dummy plugin that we can dynamically load (the InstallationJob will force a restart if one is needed):
            JSONObject cfg = new JSONObject().
                    element("name", baseName).
                    element("version", "0"). // unused but mandatory
                    element("url", t.toURI().toString()).
                    element("dependencies", dependencies);
            new UpdateSite(UpdateCenter.ID_UPLOAD, null).new Plugin(UpdateCenter.ID_UPLOAD, cfg).deploy(true);
            return new HttpRedirect("../updateCenter");
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }
    }
@ RequirePOST public HttpResponse doUploadPlugin ( StaplerRequest req ) throws IOException , ServletException { try { Jenkins . getInstance ( ) . checkPermission ( UPLOAD_PLUGINS ) ; ServletFileUpload upload = new ServletFileUpload ( new DiskFileItemFactory ( ) ) ; FileItem fileItem = upload . parseRequest ( req ) . get ( 0 ) ; String fileName = Util . getFileName ( fileItem . getName ( ) ) ; if ( "" . equals ( fileName ) ) { return new HttpRedirect ( "advanced" ) ; } if ( ! fileName . endsWith ( ".jpi" ) && ! fileName . endsWith ( ".hpi" ) ) { throw new Failure ( hudson . model . Messages . Hudson_NotAPlugin ( fileName ) ) ; } File t = File . createTempFile ( "uploaded" , ".jpi" ) ; t . deleteOnExit ( ) ; try { fileItem . write ( t ) ; } catch ( Exception e ) { throw new ServletException ( e ) ; } fileItem . delete ( ) ; final String baseName = identifyPluginShortName ( t ) ; pluginUploaded = true ; JSONArray dependencies = new JSONArray ( ) ; try { Manifest m = new JarFile ( t ) . getManifest ( ) ; String deps = m . getMainAttributes ( ) . getValue ( "Plugin-Dependencies" ) ; if ( StringUtils . isNotBlank ( deps ) ) { String [ ] plugins = deps . split ( "," ) ; for ( String p : plugins ) { String [ ] attrs = p . split ( "[:;]" ) ; dependencies . add ( new JSONObject ( ) . element ( "name" , attrs [ 0 ] ) . element ( "version" , attrs [ 1 ] ) . element ( "optional" , p . contains ( "resolution:=optional" ) ) ) ; } } } catch ( IOException e ) { LOGGER . log ( WARNING , "Unable to setup dependency list for plugin upload" , e ) ; } JSONObject cfg = new JSONObject ( ) . element ( "name" , baseName ) . element ( "version" , "0" ) . element ( "url" , t . toURI ( ) . toString ( ) ) . element ( "dependencies" , dependencies ) ; new UpdateSite ( UpdateCenter . ID_UPLOAD , null ) . new Plugin ( UpdateCenter . ID_UPLOAD , cfg ) . deploy ( true ) ; return new HttpRedirect ( "../updateCenter" ) ; } catch ( FileUploadException e ) { throw new ServletException ( e ) ; } }
Uploads a plugin.