List of usage examples for org.apache.commons.io IOExceptionWithCause IOExceptionWithCause
public IOExceptionWithCause(String message, Throwable cause)
From source file:org.openestate.is24.restapi.utils.ExportPool.java
/** * Add a real estate to export pool.//from ww w . j a v a 2 s. co m * * @param object * real estate * * @param pooledObjectId * ID of the real estate within the pool * * @throws IOException * if pooling failed */ public synchronized void putObject(RealEstate object, String pooledObjectId) throws IOException { if (object == null) return; if (pooledObjectId == null) pooledObjectId = object.getExternalId(); if (StringUtils.isBlank(pooledObjectId)) throw new IOException("No pool ID was provided for the object!"); if (StringUtils.isBlank(object.getExternalId())) object.setExternalId(pooledObjectId); final File objectDir = new File(this.objectsDir, pooledObjectId); if (objectDir.exists()) FileUtils.deleteDirectory(objectDir); if (!objectDir.mkdirs()) throw new IOException("Can't create folder at '" + objectDir.getAbsolutePath() + "'!"); OutputStream output = null; try { output = new FileOutputStream(new File(objectDir, "object.xml")); XmlUtils.writeXml(object, output); this.putSetting("object." + object.getExternalId(), UPDATE); } catch (JAXBException ex) { throw new IOExceptionWithCause("Can't write XML for object '" + pooledObjectId + "'!", ex); } finally { IOUtils.closeQuietly(output); } }
From source file:org.openestate.is24.restapi.utils.ExportPool.java
/** * Add an attachment for a real estate to export pool. * * @param pooledObjectId//from w ww . ja v a 2 s . c o m * ID of the real estate within the pool * * @param attachment * attachment informations * * @param file * attached file * * @throws IOException * if pooling failed */ public synchronized void putObjectAttachedFile(String pooledObjectId, Attachment attachment, File file) throws IOException { if (StringUtils.isBlank(pooledObjectId) || attachment == null || file == null || !file.isFile()) return; final File objectDir = new File(this.objectsDir, pooledObjectId); if (!objectDir.exists() && !objectDir.mkdirs()) throw new IOException("Can't create folder at '" + objectDir.getAbsolutePath() + "'!"); int attachmentCount = 0; File[] files = objectDir.listFiles(); if (ArrayUtils.isNotEmpty(files)) { for (File f : files) { String n = f.getName(); if (n.startsWith("attachment.") && n.endsWith(".xml")) attachmentCount++; } } final File destFile = new File(objectDir, file.getName()); FileUtils.copyFile(file, destFile); attachment.setHref(new URL("file://" + destFile.getName())); OutputStream output = null; try { output = new FileOutputStream(new File(objectDir, "attachment." + (attachmentCount + 1) + ".xml")); XmlUtils.writeXml(attachment, output); } catch (JAXBException ex) { throw new IOExceptionWithCause("Can't write XML for an attachment of object '" + pooledObjectId + "'!", ex); } finally { IOUtils.closeQuietly(output); } }
From source file:org.openestate.is24.restapi.utils.ExportPool.java
/** * Add an attachment for a real estate to export pool. * * @param pooledObjectId/*from w w w .j a v a 2 s .c o m*/ * ID of the real estate within the pool * * @param attachment * attachment informations * * @param file * URL, that points to the attached file * * @throws IOException * if pooling failed */ public synchronized void putObjectAttachedFile(String pooledObjectId, Attachment attachment, URL file) throws IOException { if (StringUtils.isBlank(pooledObjectId) || attachment == null || file == null) return; final File objectDir = new File(this.objectsDir, pooledObjectId); if (!objectDir.exists() && !objectDir.mkdirs()) throw new IOException("Can't create folder at '" + objectDir.getAbsolutePath() + "'!"); int attachmentCount = 0; File[] files = objectDir.listFiles(); if (ArrayUtils.isNotEmpty(files)) { for (File f : files) { String n = f.getName(); if (n.startsWith("attachment.") && n.endsWith(".xml")) attachmentCount++; } } attachment.setHref(file); OutputStream output = null; try { output = new FileOutputStream(new File(objectDir, "attachment." + (attachmentCount + 1) + ".xml")); XmlUtils.writeXml(attachment, output); } catch (JAXBException ex) { throw new IOExceptionWithCause("Can't write XML for an attachment of object '" + pooledObjectId + "'!", ex); } finally { IOUtils.closeQuietly(output); } }
From source file:org.openestate.is24.restapi.utils.ExportPool.java
/** * Add an web link for a real estate to export pool. * * @param pooledObjectId/*w w w .j a va 2 s . c om*/ * ID of the real estate within the pool * * @param link * web link * * @throws IOException * if pooling failed */ public synchronized void putObjectAttachedLink(String pooledObjectId, Attachment link) throws IOException { if (StringUtils.isBlank(pooledObjectId) || link == null) return; final File objectDir = new File(this.objectsDir, pooledObjectId); if (!objectDir.exists() && !objectDir.mkdirs()) throw new IOException("Can't create folder at '" + objectDir.getAbsolutePath() + "'!"); int attachmentCount = 0; File[] files = objectDir.listFiles(); if (ArrayUtils.isNotEmpty(files)) { for (File f : files) { String n = f.getName(); if (n.startsWith("attachment.") && n.endsWith(".xml")) attachmentCount++; } } OutputStream output = null; try { output = new FileOutputStream(new File(objectDir, "attachment." + (attachmentCount + 1) + ".xml")); XmlUtils.writeXml(link, output); } catch (JAXBException ex) { throw new IOExceptionWithCause("Can't write XML for an attachment of object '" + pooledObjectId + "'!", ex); } finally { IOUtils.closeQuietly(output); } }
From source file:org.openestate.is24.restapi.webapp.VerificationServlet.java
/** * Create a verification request and show a page with the verification link. * * @param req incoming GET request/*from ww w.j a v a 2 s . c om*/ * @param resp outgoing response * @throws IOException * @throws ServletException */ protected void doVerificationRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Verification verification = null; try { verification = this.getWebserviceVerification(req); } catch (OAuthException ex) { throw new IOExceptionWithCause("Can't fetch verification!", ex); } // store verification informations this.storeVerification(verification); // put verification informations into request attributes req.setAttribute("url", verification.verificationUrl); req.setAttribute("token", verification.requestToken); req.setAttribute("secret", verification.requestTokenSecret); // show JSP view resp.setCharacterEncoding("UTF-8"); RequestDispatcher d = req.getRequestDispatcher("/WEB-INF/views/verification-request.jsp"); d.include(req, resp); }
From source file:org.openestate.is24.restapi.webapp.VerificationServlet.java
/** * Creates access credentials after the verification was passed. * <p>/*www.jav a2s . co m*/ * The generated access token and secret is shown to the user by default. * * @param state the state of verification, that was received as request * parameter by the webservice * * @param token the verification token, that was received as request parameter * by the webservice * * @param verifier the verification code, that was received as request * parameter by the webservice * * @param req incoming GET request * @param resp outgoing response * @throws IOException * @throws ServletException */ protected void doVerificationResponse(String state, String token, String verifier, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { if (!"authorized".equalsIgnoreCase(state)) { req.setAttribute("authorized", false); } else { req.setAttribute("authorized", true); String secret = this.fetchVerificationSecret(token); if (token == null || secret == null) { req.setAttribute("valid", false); } else { Authorization authorization = null; try { authorization = getWebserviceAuthorization(token, secret, verifier, req); } catch (OAuthException ex) { throw new IOExceptionWithCause("Can't fetch authorization!", ex); } req.setAttribute("valid", true); req.setAttribute("token", authorization.accessToken); req.setAttribute("secret", authorization.accessTokenSecret); } } // show JSP view resp.setCharacterEncoding("UTF-8"); RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/verification-response.jsp"); dispatcher.include(req, resp); } finally { this.removeVerificationSecret(token); } }
From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java
/** * {@inheritDoc}//ww w .j a v a 2s . c o m * * Creates a ByteArrayInputStream implementation of InputStream of the XML marshaled version * of the Requisition class. Calling close on this stream is safe. */ @Override public InputStream getInputStream() throws IOException { InputStream stream = null; try { Requisition r = buildRequisitionFromZoneTransfer(); stream = new ByteArrayInputStream(jaxBMarshal(r).getBytes()); } catch (IOException e) { LOG.warn("getInputStream: Problem getting input stream", e); throw e; } catch (Throwable e) { String message = "Problem getting input stream: " + e; LOG.warn(message, e); throw new IOExceptionWithCause(message, e); } return stream; }
From source file:org.opennms.netmgt.provision.service.vmware.VmwareRequisitionUrlConnection.java
/** * {@inheritDoc}// w ww .j av a 2 s . co m * <p/> * Creates a ByteArrayInputStream implementation of InputStream of the XML * marshaled version of the Requisition class. Calling close on this stream * is safe. */ @Override public InputStream getInputStream() throws IOException { InputStream stream = null; try { logger.debug("Getting existing requisition (if any) for VMware management server {}", m_hostname); Requisition curReq = getExistingRequisition(); logger.debug("Building new requisition for VMware management server {}", m_hostname); Requisition newReq = buildVMwareRequisition(); logger.debug("Finished building new requisition for VMware management server {}", m_hostname); if (curReq == null) { if (newReq == null) { // FIXME Is this correct ? This is the old behavior newReq = new Requisition(m_foreignSource); } } else { if (newReq == null) { // If there is a requisition and the vCenter is not responding for some reason, it is better to use the old requisition, // instead of returning an empty one, which can cause the lost of all the nodes from the DB. newReq = curReq; } else { // If there is already a requisition, retrieve the custom assets and categories from the old one, and put them on the new one. // The VMWare related assets and categories will be preserved. for (RequisitionNode newNode : newReq.getNodes()) { for (RequisitionNode curNode : curReq.getNodes()) { if (newNode.getForeignId().equals(curNode.getForeignId())) { // Add existing custom assets for (RequisitionAsset asset : curNode.getAssets()) { if (!asset.getName().startsWith("vmware")) { newNode.putAsset(asset); } } // Add existing custom categories for (RequisitionCategory cat : curNode.getCategories()) { if (!cat.getName().startsWith("VMWare")) { newNode.putCategory(cat); } } // Add existing custom services /* * For each interface on the new requisition, * - Retrieve the list of custom services from the corresponding interface on the existing requisition, * matching the interface by the IP address * - If the list of services is not empty, add them to the new interface */ for (RequisitionInterface intf : curNode.getInterfaces()) { List<RequisitionMonitoredService> services = getManualyConfiguredServices(intf); if (!services.isEmpty()) { RequisitionInterface newIntf = getRequisitionInterface(newNode, intf.getIpAddr()); if (newIntf != null) { newIntf.getMonitoredServices().addAll(services); } } } } } } } } stream = new ByteArrayInputStream(jaxBMarshal(newReq).getBytes()); } catch (Throwable e) { logger.warn("Problem getting input stream: '{}'", e); throw new IOExceptionWithCause("Problem getting input stream: " + e, e); } return stream; }
From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java
/** * Reads token of the specified JsonParser and returns a list of column metadata. * * @param jsonParser the jsonParser whose next tokens are supposed to represent a list of column metadata * @return The column metadata parsed from JSON parser. * @throws IOException In case of JSON exception related error. *///from w ww. j a v a 2 s . c o m private static List<ColumnMetadata> parseAnArrayOfColumnMetadata(JsonParser jsonParser) throws IOException { try { List<ColumnMetadata> columns = new ArrayList<>(); // skip the array beginning [ jsonParser.nextToken(); while (jsonParser.nextToken() != JsonToken.END_ARRAY && !jsonParser.isClosed()) { ColumnMetadata columnMetadata = jsonParser.readValueAs(ColumnMetadata.class); columns.add(columnMetadata); } if (columns.isEmpty()) { throw new IllegalArgumentException( "No column metadata has been retrieved when trying to parse the retrieved data set."); } return columns; } catch (IOException e) { throw new IOExceptionWithCause("Unable to parse and retrieve the list of column metadata", e); } }
From source file:org.talend.dataprep.api.dataset.LightweightExportableDataSetUtils.java
private static RowMetadata parseDataSetMetadataAndReturnRowMetadata(JsonParser jsonParser) throws IOException { try {//from ww w .j ava2 s. c o m RowMetadata rowMetadata = null; while (jsonParser.nextToken() != JsonToken.END_OBJECT && !jsonParser.isClosed()) { String currentField = jsonParser.getCurrentName(); if (StringUtils.equalsIgnoreCase("columns", currentField)) { rowMetadata = new RowMetadata(parseAnArrayOfColumnMetadata(jsonParser)); } } LOGGER.debug("Skipping data to go back to the outer json object"); while (jsonParser.getParsingContext().getParent().getCurrentName() != null && !jsonParser.isClosed()) { jsonParser.nextToken(); } return rowMetadata; } catch (IOException e) { throw new IOExceptionWithCause("Unable to parse and retrieve the row metadata", e); } }