00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.openmobileis.modules.common.log.server;
00030
00031 import java.io.BufferedOutputStream;
00032 import java.io.File;
00033 import java.io.FileOutputStream;
00034 import java.io.OutputStream;
00035
00036 import org.openmobileis.common.context.SessionContext;
00037 import org.openmobileis.common.context.SessionContextManager;
00038 import org.openmobileis.common.util.codec.GeneralCoder;
00039 import org.openmobileis.common.util.log.LogManager;
00040 import org.openmobileis.synchro.openmsp.OpenMSPException;
00041 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00042 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00043 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00044 import org.openmobileis.synchro.openmsp.protocol.Element;
00045 import org.openmobileis.synchro.openmsp.protocol.Status;
00046 import org.openmobileis.synchro.openmsp.server.synctarget.OpenMSPSynchroTargetListener;
00047 import org.openmobileis.synchro.openmsp.server.synctarget.SyncTargetAnswer;
00048 import org.openmobileis.synchro.security.auth.Credential;
00049
00059 public final class LogSyncService implements OpenMSPSynchroTargetListener {
00060
00061 public LogSyncService() {
00062
00063 }
00064
00065 public SyncTargetAnswer processCommand(Credential cred, ContainerMessage containerMessage) throws OpenMSPException {
00066 AbstractCommand syncCommand = (AbstractCommand) containerMessage.getElement();
00067 int commandType = syncCommand.getElementType();
00068 Status status = new Status(syncCommand.getCmdId(), Status.STATUS_WRONG_FORMAT);
00069 if (commandType == Element.SYNC) {
00070
00071 while (containerMessage.hasMoreMessage()) {
00072 ContainerMessage commandContainer = containerMessage.nextMessage();
00073 Element command = commandContainer.getElement();
00074 if (command.getElementType() == Element.ADD) {
00075 this.processAddCommand(commandContainer);
00076 status = new Status(syncCommand.getCmdId(), Status.STATUS_OK);
00077 }
00078 }
00079 }
00080 SyncTargetAnswer answer = new SyncTargetAnswer();
00081 status.setCmdRef(syncCommand.getCmdId());
00082 ContainerMessage[] returnContainer = new ContainerMessage[1];
00083 returnContainer[0] = new ContainerMessage(status);
00084 answer.containerMessage = returnContainer;
00085 return answer;
00086 }
00087
00088 private Status processAddCommand(ContainerMessage addContainer) {
00089 try {
00090 SessionContext context = SessionContextManager.getManager().getSessionContext();
00091 String rep = context.getUserId();
00092 String basePath = System.getProperty("user.dir");
00093 Status resStat = null;
00094 String filePath = basePath + File.separator + "pdalogs" + File.separator + rep;
00095 while (addContainer.hasMoreMessage()) {
00096 DataItem item = (DataItem) addContainer.nextMessage().getElement();
00097 String fileName = item.getMetaInformation();
00098 byte[] fileData = item.getData().getBytes();
00099 fileData = GeneralCoder.decodeBase64(fileData);
00100 File newFile = new File(filePath);
00101 if (!newFile.exists()) {
00102 newFile.mkdirs();
00103 }
00104 OutputStream destFile = new BufferedOutputStream(new FileOutputStream(filePath + File.separator + fileName, true));
00105 try {
00106 destFile.write(fileData);
00107 } finally {
00108 destFile.flush();
00109 destFile.close();
00110 }
00111 }
00112 resStat = new Status(addContainer.getElement().getCmdId(), Status.STATUS_OK);
00113 return resStat;
00114 } catch (Throwable ex) {
00115 LogManager.traceError(0, ex);
00116 return new Status(addContainer.getElement().getCmdId(), Status.STATUS_FAILED);
00117 }
00118 }
00119
00120
00121
00122
00123 public String getTargetName() {
00124 return "FWKSyncLogsManager";
00125 }
00126
00127 }