Example usage for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause

List of usage examples for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause

Introduction

In this page you can find the example usage for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause.

Prototype

public IOExceptionWithCause(Throwable cause) 

Source Link

Document

Constructs a new instance with the given cause.

Usage

From source file:de.thischwa.pmcms.server.PreviewServlet.java

@Override
protected void doRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String idString = req.getParameter("id");
    if (StringUtils.isBlank(idString) || !StringUtils.isNumeric(idString))
        throw new IllegalArgumentException("'id' or type descriptor not found!");
    Integer id = Integer.valueOf(idString);

    SiteHolder siteHolder = InitializationManager.getBean(SiteHolder.class);
    APoormansObject<?> po = siteHolder.get(id);
    if (po == null)
        throw new NullPointerException("Can't work with po is null!");
    if (InstanceUtil.isSiteResource(po)) {
        try {//  www  .j ava  2s  .c o  m
            String html = velocityRenderer.render((ASiteResource) po);
            resp.getWriter().append(html);
        } catch (RenderingException e) {
            throw new IOExceptionWithCause(e);
        }
    } else {
        try {
            velocityRenderer.render(resp.getWriter(), (IRenderable) po, ViewMode.PREVIEW);
        } catch (RenderingException e) {
            throw new IOExceptionWithCause(e);
        }
    }
}

From source file:de.thischwa.pmcms.server.EditorServlet.java

@Override
protected void doRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String idString = req.getParameter("id");
    Integer id = (StringUtils.isNotBlank(idString)) ? Integer.valueOf(idString) : null;
    Class<?> cls = ContextUtil.getClass(req);

    APoormansObject<?> po;/*from   w ww .ja  va  2  s . com*/
    if (id != null && id != APoormansObject.UNSET_VALUE)
        po = siteHolder.get(id);
    else
        try {
            po = (APoormansObject<?>) cls.newInstance();
        } catch (Exception e) {
            throw new IOExceptionWithCause(e);
        }

    if (InstanceUtil.isSiteResource(po)) {
        try {
            String html = velocityRenderer.render((ASiteResource) po);
            resp.getWriter().append(html);
        } catch (RenderingException e) {
            throw new IOExceptionWithCause(e);
        }
    } else {
        Page page = (Page) po;
        logger.debug("Try to create an editor for: " + page);
        PojoHelper pojoHolder = new PojoHelper();
        pojoHolder.putpo(page);
        Map<String, Object> addObjs = new HashMap<String, Object>();
        addObjs.put("editor", new CKEditorWrapper(PoInfo.getSite(page), req));
        Map<String, Object> ctxObjs = ContextObjectManager.get(pojoHolder, ViewMode.EDIT);
        ctxObjs.putAll(addObjs);

        try {
            velocityRenderer.render(resp.getWriter(), page, ViewMode.EDIT, ctxObjs);
        } catch (RenderingException e) {
            throw new IOExceptionWithCause(e);
        }
    }

    resp.setHeader("Cache-Control", "no-cache");
}

From source file:de.thischwa.pmcms.server.ContentSaverServlet.java

@Override
protected void doRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    Map<String, String> params = new HashMap<String, String>();
    Enumeration<?> paramNames = req.getParameterNames();
    while (paramNames.hasMoreElements()) {
        Object paramName = paramNames.nextElement();
        String name = Utils.stripNonValidXMLCharacters(paramName.toString());
        String value = Utils.stripNonValidXMLCharacters(req.getParameter(name));
        params.put(name, value);/* w  w w  .  j  a v a 2s  .  com*/
    }
    APoormansObject<?> po = ContextUtil.updatepo(params);
    logger.info("Saved pojo: " + po.toString());

    try {
        if (InstanceUtil.isSiteResource(po))
            velocityRenderer.render(resp.getWriter(), (ASiteResource) po);
        else
            velocityRenderer.render(resp.getWriter(), (IRenderable) po, ViewMode.PREVIEW);
    } catch (RenderingException e) {
        throw new IOExceptionWithCause(e);
    }

    resp.setHeader("Cache-Control", "no-cache");
}

From source file:org.apache.jackrabbit.core.query.lucene.ConsistencyCheck.java

/**
 * Repairs detected errors during the consistency check.
 * @param ignoreFailure if <code>true</code> repair failures are ignored,
 *   the repair continues without throwing an exception. If
 *   <code>false</code> the repair procedure is aborted on the first
 *   repair failure.// w w w  .  j a  va 2 s .c  om
 * @throws IOException if a repair failure occurs.
 */
public void repair(boolean ignoreFailure) throws IOException {
    if (errors.size() == 0) {
        log.info("No errors found.");
        return;
    }
    int notRepairable = 0;
    for (ConsistencyCheckError error : errors) {
        try {
            if (error.repairable()) {
                error.repair();
            } else {
                log.warn("Not repairable: " + error);
                notRepairable++;
            }
        } catch (Exception e) {
            if (ignoreFailure) {
                log.warn("Exception while repairing: " + error, e);
            } else if (e instanceof IOException) {
                throw (IOException) e;
            } else {
                throw new IOExceptionWithCause(e);
            }
        }
    }
    log.info("Repaired " + (errors.size() - notRepairable) + " errors.");
    if (notRepairable > 0) {
        log.warn("" + notRepairable + " error(s) not repairable.");
    }
}

From source file:org.apache.tika.parser.jdbc.AbstractDBParser.java

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    connection = getConnection(stream, metadata, context);
    XHTMLContentHandler xHandler = null;
    List<String> tableNames = null;
    try {/*  ww w .j  av a  2 s  . c  o m*/
        tableNames = getTableNames(connection, metadata, context);
    } catch (SQLException e) {
        try {
            close();
        } catch (SQLException sqlE) {
            //swallow
        }
        throw new IOExceptionWithCause(e);
    }
    for (String tableName : tableNames) {
        //add table names to parent metadata
        metadata.add(Database.TABLE_NAME, tableName);
    }
    xHandler = new XHTMLContentHandler(handler, metadata);
    xHandler.startDocument();

    try {
        for (String tableName : tableNames) {
            JDBCTableReader tableReader = getTableReader(connection, tableName, context);
            xHandler.startElement("table", "name", tableReader.getTableName());
            xHandler.startElement("thead");
            xHandler.startElement("tr");
            for (String header : tableReader.getHeaders()) {
                xHandler.startElement("th");
                xHandler.characters(header);
                xHandler.endElement("th");
            }
            xHandler.endElement("tr");
            xHandler.endElement("thead");
            xHandler.startElement("tbody");
            while (tableReader.nextRow(xHandler, context)) {
                //no-op
            }
            xHandler.endElement("tbody");
            xHandler.endElement("table");
        }
    } finally {
        try {
            close();
        } catch (IOException | SQLException e) {
            //swallow
        }
        if (xHandler != null) {
            xHandler.endDocument();
        }
    }
}

From source file:org.apache.tika.parser.jdbc.AbstractDBParser.java

/**
 * Override this for special configuration of the connection, such as limiting
 * the number of rows to be held in memory.
 *
 * @param stream   stream to use/* ww  w.j ava 2 s .c o m*/
 * @param metadata metadata that could be used in parameterizing the connection
 * @param context  parsecontext that could be used in parameterizing the connection
 * @return connection
 * @throws java.io.IOException
 * @throws org.apache.tika.exception.TikaException
 */
protected Connection getConnection(InputStream stream, Metadata metadata, ParseContext context)
        throws IOException, TikaException {
    String connectionString = getConnectionString(stream, metadata, context);

    Connection connection = null;
    try {
        Class.forName(getJDBCClassName());
    } catch (ClassNotFoundException e) {
        throw new TikaException(e.getMessage());
    }
    try {
        connection = DriverManager.getConnection(connectionString);
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    return connection;
}

From source file:org.apache.tika.parser.jdbc.JDBCTableReader.java

public boolean nextRow(ContentHandler handler, ParseContext context) throws IOException, SAXException {
    //lazy initialization
    if (results == null) {
        reset();// w w w  . ja  v  a 2 s  . c o  m
    }
    try {
        if (!results.next()) {
            return false;
        }
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    try {
        ResultSetMetaData meta = results.getMetaData();
        handler.startElement(XHTMLContentHandler.XHTML, "tr", "tr", EMPTY_ATTRIBUTES);
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            handler.startElement(XHTMLContentHandler.XHTML, "td", "td", EMPTY_ATTRIBUTES);
            handleCell(meta, i, handler, context);
            handler.endElement(XHTMLContentHandler.XHTML, "td", "td");
        }
        handler.endElement(XHTMLContentHandler.XHTML, "tr", "tr");
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    rows++;
    return true;
}

From source file:org.apache.tika.parser.jdbc.JDBCTableReader.java

public List<String> getHeaders() throws IOException {
    List<String> headers = new LinkedList<String>();
    //lazy initialization
    if (results == null) {
        reset();//from  w w  w  . j  a  v  a  2  s  .c o  m
    }
    try {
        ResultSetMetaData meta = results.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            headers.add(meta.getColumnName(i));
        }
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    return headers;
}

From source file:org.apache.tika.parser.jdbc.JDBCTableReader.java

void reset() throws IOException {

    if (results != null) {
        try {//from  w ww  . j av  a2 s . c om
            results.close();
        } catch (SQLException e) {
            //swallow
        }
    }

    String sql = "SELECT * from " + tableName;
    try {
        Statement st = connection.createStatement();
        results = st.executeQuery(sql);
    } catch (SQLException e) {
        throw new IOExceptionWithCause(e);
    }
    rows = 0;
}

From source file:org.apache.tika.parser.jdbc.SQLite3DBParser.java

@Override
protected Connection getConnection(InputStream stream, Metadata metadata, ParseContext context)
        throws IOException {
    String connectionString = getConnectionString(stream, metadata, context);

    Connection connection = null;
    try {/*  w w  w .ja v  a2s.  c  o  m*/
        Class.forName(getJDBCClassName());
    } catch (ClassNotFoundException e) {
        throw new IOExceptionWithCause(e);
    }
    try {
        SQLiteConfig config = new SQLiteConfig();

        //good habit, but effectively meaningless here
        config.setReadOnly(true);
        connection = config.createConnection(connectionString);

    } catch (SQLException e) {
        throw new IOException(e.getMessage());
    }
    return connection;
}