List of usage examples for javax.activation MimetypesFileTypeMap MimetypesFileTypeMap
public MimetypesFileTypeMap()
From source file:org.etudes.jforum.view.admin.ImportExportAction.java
/** * check and save resource/* w w w . ja v a 2 s. c o m*/ * @param refId * - reference id * @param unZippedDirPath * - unzipped directory path * @param htmlText * - html text */ private void checkAndSaveResource(String refId, String unZippedDirPath, String htmlText) { if (refId == null || refId.trim().length() == 0) return; String siteId = ToolManager.getCurrentPlacement().getContext(); boolean exisResource = false; // get the resource try { // bypass security when reading the resource to copy SecurityService.pushAdvisor(new SecurityAdvisor() { public SecurityAdvice isAllowed(String userId, String function, String reference) { return SecurityAdvice.ALLOWED; } }); //ContentResource resource = ContentHostingService.getResource("/group/" + siteId + refId); ContentHostingService.checkResource("/group/" + siteId + refId); exisResource = true; } catch (PermissionException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); return; } catch (IdUnusedException e) { /*if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString());*/ } catch (TypeException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); return; } finally { SecurityService.popAdvisor(); } if (logger.isDebugEnabled()) logger.debug("Resource is " + exisResource); if (!exisResource) { // bypass security when reading the resource to copy SecurityService.pushAdvisor(new SecurityAdvisor() { public SecurityAdvice isAllowed(String userId, String function, String reference) { return SecurityAdvice.ALLOWED; } }); // the new resource collection and name String destinationCollection = "/group/" + siteId + refId; String displayName = refId.substring(refId.lastIndexOf("/") + 1); // make sure to remove the reference root sakai:reference-root ResourcePropertiesEdit props = ContentHostingService.newResourceProperties(); props.removeProperty("sakai:reference-root"); props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, displayName); File file = new File(unZippedDirPath + File.separator + "resources" + File.separator + "embeded_jf_content/content/group/" + refId); byte[] body; body = new String(htmlText).getBytes(); String type = new MimetypesFileTypeMap().getContentType(file); ContentResource importedResource = null; try { String destinationPath = destinationCollection; importedResource = ContentHostingService.addResource(destinationPath, type, body, props, 0); } catch (IdInvalidException e) { if (logger.isErrorEnabled()) logger.error("Error while adding resource : " + refId); return; } catch (InconsistentException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); } catch (OverQuotaException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); } catch (ServerOverloadException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); } catch (PermissionException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); } catch (IdUsedException e) { if (logger.isWarnEnabled()) logger.warn("checkAndSaveResource: " + e.toString()); } finally { SecurityService.popAdvisor(); } } }