Эх сурвалжийг харах

实现jasperserver axis新增文件接口

sunyj 8 жил өмнө
parent
commit
26953b7244

+ 8 - 0
src/main/java/com/uas/report/axis/repository/RepositoryManagementService.java

@@ -1,5 +1,11 @@
 package com.uas.report.axis.repository;
 
+import org.apache.axis.AxisFault;
+import org.apache.axis.attachments.AttachmentPart;
+import org.springframework.context.NoSuchMessageException;
+
+import com.uas.report.axis.WSException;
+
 public interface RepositoryManagementService {
 
 	public String list(String requestXmlString);
@@ -15,4 +21,6 @@ public interface RepositoryManagementService {
 	public String move(String requestXmlString);
 
 	public String copy(String requestXmlString);
+
+	public AttachmentPart[] getMessageAttachments() throws NoSuchMessageException, WSException, AxisFault;
 }

+ 54 - 41
src/main/java/com/uas/report/axis/repository/RepositoryManagementServiceImpl.java

@@ -3,6 +3,7 @@ package com.uas.report.axis.repository;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.ArrayList;
@@ -43,6 +44,7 @@ import com.uas.report.axis.Resource;
 import com.uas.report.axis.ResourceDescriptor;
 import com.uas.report.axis.BasicResourceHandler;
 import com.uas.report.axis.ResourceHandlerRegistry;
+import com.uas.report.axis.ResourceProperty;
 import com.uas.report.axis.ResultAttachments;
 import com.uas.report.axis.ValidationError;
 import com.uas.report.axis.ValidationErrors;
@@ -50,14 +52,16 @@ import com.uas.report.axis.WSException;
 import com.uas.report.axis.WSValidationException;
 import com.uas.report.axis.util.Marshaller;
 import com.uas.report.axis.util.Unmarshaller;
+import com.uas.report.util.ContextUtils;
+import com.uas.report.util.FileUtils;
 
 @Service
 public class RepositoryManagementServiceImpl implements RepositoryManagementService, InitializingBean {
 
 	private static final String VERSION = "2.0.1";
 
-	@Autowired
-	private SpecialProperties specialProperties;
+	// @Autowired
+	// private SpecialProperties specialProperties;
 
 	private Locale locale = null; // Default locale....
 
@@ -301,6 +305,16 @@ public class RepositoryManagementServiceImpl implements RepositoryManagementServ
 
 			ResourceDescriptor newDescriptor = request.getResourceDescriptor();
 
+			// 获取上传的数据
+			AttachmentPart[] attachmentParts = getMessageAttachments();
+			if (attachmentParts.length > 1) {
+				throw new IllegalStateException("不可上传多个文件");
+			} else if (attachmentParts.length == 1) {
+				InputStream inputStream = attachmentParts[0].getDataHandler().getInputStream();
+				byte[] data = FileUtils.readData(inputStream);
+				newDescriptor.setData(data);
+			}
+
 			logger.debug("Put: for " + newDescriptor.getUriString());
 
 			if (newDescriptor.getUriString() == null || newDescriptor.getUriString().length() == 0) {
@@ -326,7 +340,6 @@ public class RepositoryManagementServiceImpl implements RepositoryManagementServ
 			operationResult.setReturnCode(e.getErrorCode());
 			operationResult.setMessage(e.getMessage());
 		} catch (Exception e) {
-
 			// e.printStackTrace();
 			logger.error("caught exception: " + e.getMessage(), e);
 
@@ -370,44 +383,44 @@ public class RepositoryManagementServiceImpl implements RepositoryManagementServ
 	 * @throws AxisFault
 	 *
 	 */
-	// public AttachmentPart[] getMessageAttachments() throws
-	// NoSuchMessageException, WSException, AxisFault {
-	// try {
-	// MessageContext msgContext = MessageContext.getCurrentContext();
-	// File file = new File(attachmentsPath);
-	// if (!file.exists()) {
-	// file.mkdirs();
-	// }
-	// msgContext.setProperty(MessageContext.ATTACHMENTS_DIR, attachmentsPath);
-	// Message reqMsg = msgContext.getRequestMessage();
-	// Attachments messageAttachments = reqMsg.getAttachmentsImpl();
-	// if (null == messageAttachments) {
-	// logger.error("no attachment support");
-	// return new AttachmentPart[0];
-	// }
-	// int attachmentCount = messageAttachments.getAttachmentCount();
-	// AttachmentPart attachments[] = new AttachmentPart[attachmentCount];
-	//
-	// Iterator<?> it = messageAttachments.getAttachments().iterator();
-	// int count = 0;
-	// while (it.hasNext()) {
-	// AttachmentPart part = (AttachmentPart) it.next();
-	// attachments[count++] = part;
-	// }
-	// return attachments;
-	// } catch (AxisFault e) {
-	// if (e.getFaultString().startsWith("java.io.IOException")) {
-	//// throw new JSExceptionWrapper(new WSException(WSException.GENERAL_ERROR,
-	//// messageSource.getMessage("webservices.error.attachments.folder", null,
-	// getLocale())));
-	// throw new WSException(WSException.GENERAL_ERROR,
-	// messageSource.getMessage("webservices.error.attachments.folder", null,
-	// getLocale()));
-	// }
-	//// throw new JSExceptionWrapper(e);
-	// throw e;
-	// }
-	// }
+	@Override
+	public AttachmentPart[] getMessageAttachments() throws NoSuchMessageException, WSException, AxisFault {
+		try {
+			MessageContext msgContext = MessageContext.getCurrentContext();
+			// File file = new File(attachmentsPath);
+			// if (!file.exists()) {
+			// file.mkdirs();
+			// }
+			// msgContext.setProperty(MessageContext.ATTACHMENTS_DIR,
+			// attachmentsPath);
+			Message reqMsg = msgContext.getRequestMessage();
+			Attachments messageAttachments = reqMsg.getAttachmentsImpl();
+			if (null == messageAttachments) {
+				logger.error("no attachment support");
+				return new AttachmentPart[0];
+			}
+			int attachmentCount = messageAttachments.getAttachmentCount();
+			AttachmentPart attachments[] = new AttachmentPart[attachmentCount];
+
+			Iterator<?> it = messageAttachments.getAttachments().iterator();
+			int count = 0;
+			while (it.hasNext()) {
+				AttachmentPart part = (AttachmentPart) it.next();
+				attachments[count++] = part;
+			}
+			return attachments;
+		} catch (AxisFault e) {
+			if (e.getFaultString().startsWith("java.io.IOException")) {
+				// throw new JSExceptionWrapper(new
+				// WSException(WSException.GENERAL_ERROR,
+				// messageSource.getMessage("webservices.error.attachments.folder",null,getLocale())));
+				throw new WSException(WSException.GENERAL_ERROR,
+						messageSource.getMessage("webservices.error.attachments.folder", null, getLocale()));
+			}
+			// throw new JSExceptionWrapper(e);
+			throw e;
+		}
+	}
 
 	public Locale getLocale() {
 		return locale;

+ 23 - 0
src/main/java/com/uas/report/util/FileUtils.java

@@ -1,11 +1,13 @@
 package com.uas.report.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.nio.ByteBuffer;
 import java.nio.channels.Channels;
@@ -262,4 +264,25 @@ public class FileUtils {
 		}
 		return fileName.substring(0, fileName.lastIndexOf("."));
 	}
+
+	/**
+	 * Read the supplied InputStream and return as a byte array.
+	 * 
+	 * @param stream
+	 *            The stream to read.
+	 * @return byte array containing the Stream data.
+	 * @throws IOException
+	 *             Exception reading from the stream.
+	 */
+	public static byte[] readData(InputStream stream) throws IOException {
+		ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+		byte[] byteBuf = new byte[1024];
+		int readCount = 0;
+
+		while ((readCount = stream.read(byteBuf)) != -1) {
+			bytesOut.write(byteBuf, 0, readCount);
+		}
+		stream.close();
+		return bytesOut.toByteArray();
+	}
 }