Search in sources :

Example 21 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class Api method doXml.

/**
     * Exposes the bean as XML.
     */
public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter String xpath, @QueryParameter String wrapper, @QueryParameter int depth) throws IOException, ServletException {
    String[] excludes = req.getParameterValues("exclude");
    if (xpath == null && excludes == null) {
        // serve the whole thing
        rsp.serveExposedBean(req, bean, Flavor.XML);
        return;
    }
    StringWriter sw = new StringWriter();
    // first write to String
    Model p = MODEL_BUILDER.get(bean.getClass());
    p.writeTo(bean, depth, Flavor.XML.createDataWriter(bean, sw));
    // apply XPath
    Object result;
    try {
        Document dom = new SAXReader().read(new StringReader(sw.toString()));
        // apply exclusions
        if (excludes != null) {
            for (String exclude : excludes) {
                List<org.dom4j.Node> list = (List<org.dom4j.Node>) dom.selectNodes(exclude);
                for (org.dom4j.Node n : list) {
                    Element parent = n.getParent();
                    if (parent != null)
                        parent.remove(n);
                }
            }
        }
        if (xpath == null) {
            result = dom;
        } else {
            List list = dom.selectNodes(xpath);
            if (wrapper != null) {
                Element root = DocumentFactory.getInstance().createElement(wrapper);
                for (Object o : list) {
                    if (o instanceof String) {
                        root.addText(o.toString());
                    } else {
                        root.add(((org.dom4j.Node) o).detach());
                    }
                }
                result = root;
            } else if (list.isEmpty()) {
                rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath));
                return;
            } else if (list.size() > 1) {
                rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size()));
                return;
            } else {
                result = list.get(0);
            }
        }
    } catch (DocumentException e) {
        throw new IOException2(e);
    }
    OutputStream o = rsp.getCompressedOutputStream(req);
    try {
        if (result instanceof CharacterData) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(((CharacterData) result).getText().getBytes("UTF-8"));
            return;
        }
        if (result instanceof String || result instanceof Number || result instanceof Boolean) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(result.toString().getBytes("UTF-8"));
            return;
        }
        // otherwise XML
        rsp.setContentType("application/xml;charset=UTF-8");
        new XMLWriter(o).write(result);
    } finally {
        o.close();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) StringWriter(java.io.StringWriter) CharacterData(org.dom4j.CharacterData) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) List(java.util.List) IOException2(hudson.util.IOException2)

Example 22 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class AnnotatedLargeText method writeHtmlTo.

public long writeHtmlTo(long start, Writer w) throws IOException {
    ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, createAnnotator(Stapler.getCurrentRequest()), context, charset);
    long r = super.writeLogTo(start, caw);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Cipher sym = Secret.getCipher("AES");
        sym.init(Cipher.ENCRYPT_MODE, Hudson.getInstance().getSecretKeyAsAES128());
        ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
        // send timestamp to prevent a replay attack
        oos.writeLong(System.currentTimeMillis());
        oos.writeObject(caw.getConsoleAnnotator());
        oos.close();
        StaplerResponse rsp = Stapler.getCurrentResponse();
        if (rsp != null)
            rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
    } catch (GeneralSecurityException e) {
        throw new IOException2(e);
    }
    return r;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) StaplerResponse(org.kohsuke.stapler.StaplerResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) ObjectOutputStream(java.io.ObjectOutputStream) IOException2(hudson.util.IOException2)

Example 23 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class AbstractItem method doConfigDotXml.

/**
     * Accepts <tt>config.xml</tt> submission, as well as serve it.
     */
@WebMethod(name = "config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp) throws IOException {
    if (req.getMethod().equals("GET")) {
        // read
        checkPermission(EXTENDED_READ);
        rsp.setContentType("application/xml");
        getConfigFile().writeRawTo(rsp.getOutputStream());
        return;
    }
    if (req.getMethod().equals("POST")) {
        // submission
        checkPermission(CONFIGURE);
        XmlFile configXmlFile = getConfigFile();
        AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
        try {
            try {
                // this allows us to use UTF-8 for storing data,
                // plus it checks any well-formedness issue in the submitted
                // data
                Transformer t = TransformerFactory.newInstance().newTransformer();
                t.transform(new StreamSource(req.getReader()), new StreamResult(out));
                out.close();
            } catch (TransformerException e) {
                throw new IOException2("Failed to persist configuration.xml", e);
            }
            // try to reflect the changes by reloading
            new XmlFile(Items.XSTREAM, out.getTemporaryFile()).unmarshal(this);
            onLoad(getParent(), getRootDir().getName());
            // if everything went well, commit this new version
            out.commit();
        } finally {
            // don't leave anything behind
            out.abort();
        }
        return;
    }
    // huh?
    rsp.sendError(SC_BAD_REQUEST);
}
Also used : Transformer(javax.xml.transform.Transformer) XmlFile(hudson.XmlFile) StreamResult(javax.xml.transform.stream.StreamResult) AtomicFileWriter(hudson.util.AtomicFileWriter) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) IOException2(hudson.util.IOException2) WebMethod(org.kohsuke.stapler.WebMethod)

Example 24 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class Job method onLoad.

@Override
@SuppressWarnings("unchecked")
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
    super.onLoad(parent, name);
    cascadingProject = (JobT) Functions.getItemByName(Hudson.getInstance().getAllItems(this.getClass()), cascadingProjectName);
    initAllowSave();
    TextFile f = getNextBuildNumberFile();
    if (f.exists()) {
        // assume that nextBuildNumber was read from config.xml
        try {
            synchronized (this) {
                this.nextBuildNumber = Integer.parseInt(f.readTrim());
            }
        } catch (NumberFormatException e) {
            throw new IOException2(f + " doesn't contain a number", e);
        }
    } else {
        // From the old Hudson, or doCreateItem. Create this file now.
        saveNextBuildNumber();
        // and delete it from the config.xml
        save();
    }
    if (// didn't exist < 1.72
    properties == null)
        properties = new CopyOnWriteList<JobProperty<? super JobT>>();
    if (cascadingChildrenNames == null) {
        cascadingChildrenNames = new CopyOnWriteArraySet<String>();
    }
    buildProjectProperties();
    for (JobProperty p : getAllProperties()) {
        p.setOwner(this);
    }
}
Also used : CopyOnWriteList(hudson.util.CopyOnWriteList) TextFile(hudson.util.TextFile) IOException2(hudson.util.IOException2)

Example 25 with IOException2

use of hudson.util.IOException2 in project hudson-2.x by hudson.

the class SmoothiePluginStrategy method createPluginWrapper.

/**
     * Load the plugins wrapper and inject it with the {@link SmoothieContainer}.
     */
public PluginWrapper createPluginWrapper(final File archive) throws IOException {
    checkNotNull(archive);
    PluginWrapper plugin;
    try {
        plugin = pluginFactory.create(archive);
    } catch (Exception e) {
        throw new IOException2(e);
    }
    if (log.isDebugEnabled()) {
        logPluginDetails(plugin);
    }
    return plugin;
}
Also used : PluginWrapper(hudson.PluginWrapper) IOException(java.io.IOException) IOException2(hudson.util.IOException2)

Aggregations

IOException2 (hudson.util.IOException2)37 IOException (java.io.IOException)20 File (java.io.File)16 FileInputStream (java.io.FileInputStream)10 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 InputStream (java.io.InputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 TarInputStream (hudson.org.apache.tools.tar.TarInputStream)3 VirtualChannel (hudson.remoting.VirtualChannel)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)3 NodeList (org.w3c.dom.NodeList)3 SAXException (org.xml.sax.SAXException)3 XmlPullParser (org.xmlpull.v1.XmlPullParser)3 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)3 StreamException (com.thoughtworks.xstream.io.StreamException)2 ObjectInputStreamEx (hudson.remoting.ObjectInputStreamEx)2