use of org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException in project carbon-business-process by wso2.
the class AttachmentUploadClient method addUploadedFileItem.
/**
* Upload the attachment and return the attachment id
* @param fileItemData wrapper for the attachment
* @return attachment id for the uploaded attachment
* @throws AttachmentMgtException If an error occurred in the back-end component
* @throws RemoteException if an error during the communication
*/
public String addUploadedFileItem(FileItemData fileItemData) throws AttachmentMgtException, RemoteException, ExceptionException {
DataHandler handler = fileItemData.getDataHandler();
TAttachment attachment = new TAttachment();
attachment.setName(handler.getName());
attachment.setContentType(handler.getContentType());
attachment.setCreatedBy(getUserName());
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
attachment.setCreatedTime(calendar);
attachment.setContent(handler);
String attachmentID = stub.add(attachment);
log.info("Attachment was uploaded with id:" + attachmentID);
return attachmentID;
}
use of org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException in project carbon-business-process by wso2.
the class AttachmentMgtDAOBasicOperationsTest method testAttachmentDAOAddTest.
/**
* This method tests the attachment upload functionality
*/
public void testAttachmentDAOAddTest() {
AttachmentManagerService service = new AttachmentManagerService();
try {
attachmentID = service.add(createAttachment());
log.info("Attachment added with id : " + attachmentID);
assertNotNull(attachmentID);
} catch (AttachmentMgtException ex) {
log.error(ex.getLocalizedMessage(), ex);
Assert.fail("Attachment upload failed due to reason: " + ex.getLocalizedMessage());
}
}
use of org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException in project carbon-business-process by wso2.
the class AttachmentDownloadServlet method doGet.
/**
* Logic that will be executed for a get request.
*
* @param request the HTTP Servlet request.
* @param response the HTTP Servlet response.
* @throws ServletException if an error occurred.
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURI();
String attachmentUniqueID = url.substring(url.lastIndexOf("/") + 1);
InputStream contentStream = null;
ServletOutputStream servletOutputStream = null;
try {
TAttachment fileAttachment = getFileFromUniqueID(url);
response.setHeader("Content-Disposition", "attachment; filename=" + fileAttachment.getName());
response.setContentType(fileAttachment.getContentType());
contentStream = fileAttachment.getContent().getInputStream();
servletOutputStream = response.getOutputStream();
IOUtils.copy(contentStream, servletOutputStream);
servletOutputStream.flush();
} catch (AttachmentMgtException e) {
throw new ServletException(e.getLocalizedMessage(), e);
} finally {
IOUtils.closeQuietly(contentStream);
IOUtils.closeQuietly(servletOutputStream);
}
}
use of org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException in project carbon-business-process by wso2.
the class AttachmentMgtDAOFactoryImpl method removeAttachment.
@Override
public boolean removeAttachment(final String id) throws AttachmentMgtException {
try {
boolean isRemoved = false;
isRemoved = jobExecutor.execTransaction(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Query query = entityManager.createQuery("DELETE FROM org.wso2.carbon.attachment.mgt.core.dao.impl" + ".jpa.openjpa.entity.AttachmentDAOImpl x WHERE x.id = " + ":attachmentID");
query.setParameter("attachmentID", Long.parseLong(id));
int noOfRowsUpdated = query.executeUpdate();
// entityManager.remove(getAttachmentInfo(id));
if (noOfRowsUpdated == 1) {
return true;
} else {
return false;
}
}
});
return isRemoved;
} catch (Exception e) {
String errorMsg = "org.wso2.carbon.attachment.mgt.core.dao.impl.hibernate.AttachmentMgtDAOFactoryImpl.removeAttachment operation failed. " + "Reason: " + e.getLocalizedMessage();
log.error(errorMsg, e);
throw new AttachmentMgtException(errorMsg, e);
}
}
use of org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException in project carbon-business-process by wso2.
the class AttachmentMgtDAOFactoryImpl method getAttachmentInfoFromURL.
@Override
public AttachmentDAO getAttachmentInfoFromURL(final String attachmentURI) throws AttachmentMgtException {
try {
AttachmentDAO resultantDAO = jobExecutor.execTransaction(new Callable<AttachmentDAO>() {
@Override
public AttachmentDAO call() throws Exception {
Query query = entityManager.createQuery("SELECT x FROM org.wso2.carbon.attachment.mgt.core.dao.impl.jpa.openjpa.entity.AttachmentDAOImpl AS x WHERE x.url = :attachmentURI");
query.setParameter("attachmentURI", attachmentURI);
List<AttachmentDAO> daoList = query.getResultList();
if (daoList.isEmpty()) {
throw new AttachmentMgtException("Attachment not found for the uri:" + attachmentURI);
} else if (daoList.size() != 1) {
String errorMsg = "There exist more than one attachment for the attachment URI:" + attachmentURI + ". org" + ".wso2.carbon.attachment.mgt.util.URLGeneratorUtil.generateURL method has generated " + "similar uris for different attachments. This has caused a major inconsistency for " + "attachment management.";
log.fatal(errorMsg);
throw new AttachmentMgtException(errorMsg);
} else {
return daoList.get(0);
}
}
});
return resultantDAO;
} catch (Exception e) {
String errorMsg = "org.wso2.carbon.attachment.mgt.core.dao.impl.hibernate.AttachmentMgtDAOFactoryImpl.getAttachmentInfoFromURL operation failed. " + "Reason: " + e.getLocalizedMessage();
log.error(errorMsg, e);
throw new AttachmentMgtException(errorMsg, e);
}
}
Aggregations