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