📄 dataresourceworker.java
字号:
if( targetMimeType.equals("text/html")) { /* if (request == null || response == null) { throw new GeneralException("Request [" + request + "] or response [" + response + "] is null."); } ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); boolean fullPath = false; boolean secure = false; boolean encode = false; String url = rh.makeLink(request, response, buf.toString(), fullPath, secure, encode); */ String img = "<img src=\"/content/control/img?imgId=" + textData + "\"/>"; out.write(img); } else if( targetMimeType.equals("text/plain")) { out.write(textData); } } } public static void renderFile(String dataResourceTypeId, String objectInfo, String rootDir, Writer out) throws GeneralException, IOException { // TODO: this method assumes the file is a text file, if it is an image we should respond differently, see the comment above for IMAGE_OBJECT type data resource if (dataResourceTypeId.equals("LOCAL_FILE")) { File file = new File(objectInfo); if (!file.isAbsolute()) { throw new GeneralException("File (" + objectInfo + ") is not absolute"); } int c; FileReader in = new FileReader(file); while ((c = in.read()) != -1) { out.write(c); } } else if (dataResourceTypeId.equals("OFBIZ_FILE")) { String prefix = System.getProperty("ofbiz.home"); String sep = ""; if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } File file = new File(prefix + sep + objectInfo); int c; FileReader in = new FileReader(file); while ((c = in.read()) != -1) out.write(c); } else if (dataResourceTypeId.equals("CONTEXT_FILE")) { String prefix = rootDir; String sep = ""; if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } File file = new File(prefix + sep + objectInfo); int c; FileReader in = null; try { in = new FileReader(file); String enc = in.getEncoding(); if (Debug.infoOn()) Debug.logInfo("in serveImage, encoding:" + enc, module); } catch (FileNotFoundException e) { Debug.logError(e, " in renderDataResourceAsHtml(CONTEXT_FILE), in FNFexception:", module); throw new GeneralException("Could not find context file to render", e); } catch (Exception e) { Debug.logError(" in renderDataResourceAsHtml(CONTEXT_FILE), got exception:" + e.getMessage(), module); } while ((c = in.read()) != -1) { out.write(c); } //out.flush(); } return; } public static String buildRequestPrefix(GenericDelegator delegator, Locale locale, String webSiteId, String https) { String prefix = null; Map prefixValues = new HashMap(); NotificationServices.setBaseUrl(delegator, webSiteId, prefixValues); if (https != null && https.equalsIgnoreCase("true")) { prefix = (String) prefixValues.get("baseSecureUrl"); } else { prefix = (String) prefixValues.get("baseUrl"); } if (UtilValidate.isEmpty(prefix)) { if (https != null && https.equalsIgnoreCase("true")) { prefix = UtilProperties.getMessage("content", "baseSecureUrl", locale); } else { prefix = UtilProperties.getMessage("content", "baseUrl", locale); } } return prefix; } public static File getContentFile(String dataResourceTypeId, String objectInfo, String rootDir) throws GeneralException, FileNotFoundException{ File file = null; if (dataResourceTypeId.equals("LOCAL_FILE") || dataResourceTypeId.equals("LOCAL_FILE_BIN")) { file = new File(objectInfo); if (!file.isAbsolute()) { throw new GeneralException("File (" + objectInfo + ") is not absolute"); } int c; } else if (dataResourceTypeId.equals("OFBIZ_FILE") || dataResourceTypeId.equals("OFBIZ_FILE_BIN")) { String prefix = System.getProperty("ofbiz.home"); String sep = ""; if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } file = new File(prefix + sep + objectInfo); } else if (dataResourceTypeId.equals("CONTEXT_FILE") || dataResourceTypeId.equals("CONTEXT_FILE_BIN")) { String prefix = rootDir; String sep = ""; if (objectInfo.indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } file = new File(prefix + sep + objectInfo); } return file; } public static String getDataResourceMimeType(GenericDelegator delegator, String dataResourceId, GenericValue view) throws GenericEntityException { String mimeType = null; if (view != null) mimeType = view.getString("drMimeTypeId"); //if (Debug.infoOn()) Debug.logInfo("getDataResourceMimeType, mimeType(2):" + mimeType, ""); if (UtilValidate.isEmpty(mimeType) && UtilValidate.isNotEmpty(dataResourceId)) { GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); //if (Debug.infoOn()) Debug.logInfo("getDataResourceMimeType, dataResource(2):" + dataResource, ""); mimeType = dataResource.getString("mimeTypeId"); } return mimeType; } public static String getDataResourceContentUploadPath() { String initialPath = UtilProperties.getPropertyValue("content.properties", "content.upload.path.prefix"); double maxFiles = UtilProperties.getPropertyNumber("content.properties", "content.upload.max.files"); if (maxFiles < 1) { maxFiles = 250; } String ofbizHome = System.getProperty("ofbiz.home"); if (!initialPath.startsWith("/")) { initialPath = "/" + initialPath; } // descending comparator Comparator desc = new Comparator() { public int compare(Object o1, Object o2) { if (((Long) o1).longValue() > ((Long) o2).longValue()) { return -1; } else if (((Long) o1).longValue() < ((Long) o2).longValue()) { return 1; } return 0; } }; // check for the latest subdirectory String parentDir = ofbizHome + initialPath; File parent = new File(parentDir); TreeMap dirMap = new TreeMap(desc); if (parent.exists()) { File[] subs = parent.listFiles(); for (int i = 0; i < subs.length; i++) { if (subs[i].isDirectory()) { dirMap.put(new Long(subs[0].lastModified()), subs[i]); } } } else { // if the parent doesn't exist; create it now boolean created = parent.mkdir(); if (!created) { Debug.logWarning("Unable to create top level upload directory [" + parentDir + "].", module); } } // first item in map is the most current directory File latestDir = null; if (dirMap != null && dirMap.size() > 0) { latestDir = (File) dirMap.values().iterator().next(); if (latestDir != null) { File[] dirList = latestDir.listFiles(); if (dirList.length >= maxFiles) { latestDir = makeNewDirectory(parent); } } } else { latestDir = makeNewDirectory(parent); } Debug.log("Directory Name : " + latestDir.getName(), module); return latestDir.getAbsolutePath().replace('\\','/'); } private static File makeNewDirectory(File parent) { File latestDir = null; boolean newDir = false; while (!newDir) { latestDir = new File(parent, "" + System.currentTimeMillis()); if (!latestDir.exists()) { latestDir.mkdir(); newDir = true; } } return latestDir; } public static void streamDataResource(OutputStream os, GenericDelegator delegator, String dataResourceId, String https, String webSiteId, Locale locale, String rootDir) throws IOException, GeneralException { try { GenericValue dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); if (dataResource == null) { throw new GeneralException("Error in streamDataResource: DataResource with ID [" + dataResourceId + "] was not found."); } String dataResourceTypeId = dataResource.getString("dataResourceTypeId"); if (UtilValidate.isEmpty(dataResourceTypeId)) { dataResourceTypeId = "SHORT_TEXT"; } String mimeTypeId = dataResource.getString("mimeTypeId"); if (UtilValidate.isEmpty(mimeTypeId)) { mimeTypeId = "text/html"; } if (dataResourceTypeId.equals("SHORT_TEXT")) { String text = dataResource.getString("objectInfo"); os.write(text.getBytes()); } else if (dataResourceTypeId.equals("ELECTRONIC_TEXT")) { GenericValue electronicText = delegator.findByPrimaryKeyCache("ElectronicText", UtilMisc.toMap("dataResourceId", dataResourceId)); if (electronicText != null) { String text = electronicText.getString("textData"); if (text != null) os.write(text.getBytes()); } } else if (dataResourceTypeId.equals("IMAGE_OBJECT")) { byte[] imageBytes = acquireImage(delegator, dataResource); if (imageBytes != null) os.write(imageBytes); } else if (dataResourceTypeId.equals("LINK")) { String text = dataResource.getString("objectInfo"); os.write(text.getBytes()); } else if (dataResourceTypeId.equals("URL_RESOURCE")) { URL url = new URL(dataResource.getString("objectInfo")); if (url.getHost() == null) { // is relative String prefix = buildRequestPrefix(delegator, locale, webSiteId, https); String sep = ""; //String s = ""; if (url.toString().indexOf("/") != 0 && prefix.lastIndexOf("/") != (prefix.length() - 1)) { sep = "/"; } String s2 = prefix + sep + url.toString(); url = new URL(s2); } InputStream in = url.openStream(); int c; while ((c = in.read()) != -1) { os.write(c); } } else if (dataResourceTypeId.indexOf("_FILE") >= 0) { String objectInfo = dataResource.getString("objectInfo"); File inputFile = getContentFile(dataResourceTypeId, objectInfo, rootDir); //long fileSize = inputFile.length(); FileInputStream fis = new FileInputStream(inputFile); int c; while ((c = fis.read()) != -1) { os.write(c); } } else { throw new GeneralException("The dataResourceTypeId [" + dataResourceTypeId + "] is not supported in streamDataResource"); } } catch(GenericEntityException e) { throw new GeneralException("Error in streamDataResource", e); } } public static ByteWrapper getContentAsByteWrapper(GenericDelegator delegator, String dataResourceId, String https, String webSiteId, Locale locale, String rootDir) throws IOException, GeneralException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); streamDataResource(baos, delegator, dataResourceId, https, webSiteId, locale, rootDir); ByteWrapper byteWrapper = new ByteWrapper(baos.toByteArray()); return byteWrapper; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -