1 31 package org.objectweb.proactive.core.body.reply; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.ObjectInputStream ; 35 36 import org.objectweb.proactive.core.UniqueID; 37 import org.objectweb.proactive.core.body.LocalBodyStore; 38 import org.objectweb.proactive.core.body.UniversalBody; 39 import org.objectweb.proactive.core.body.message.MessageImpl; 40 import org.objectweb.proactive.core.mop.Utils; 41 import org.objectweb.proactive.ext.security.ProActiveSecurityManager; 42 import org.objectweb.proactive.ext.security.RenegotiateSessionException; 43 import org.objectweb.proactive.ext.security.SecurityContext; 44 import org.objectweb.proactive.ext.security.SecurityNotAvailableException; 45 46 47 public class ReplyImpl extends MessageImpl implements Reply, java.io.Serializable { 48 49 52 protected Object result; 53 54 56 59 protected byte[][] encryptedResult; 60 61 62 65 protected long sessionID; 66 protected transient ProActiveSecurityManager psm = null; 67 68 public ReplyImpl(UniqueID senderID, long sequenceNumber, String methodName, Object result, ProActiveSecurityManager psm) { 69 super(senderID, sequenceNumber, true, methodName); 70 this.result = result; 71 this.psm = psm; 72 } 73 74 public Object getResult() { 75 return result; 76 } 77 78 public void send(UniversalBody destinationBody) throws java.io.IOException { 79 UniqueID destinationID = destinationBody.getID(); 82 boolean isLocal = ((LocalBodyStore.getInstance().getLocalBody(destinationID) != null) 83 || (LocalBodyStore.getInstance().getLocalHalfBody(destinationID) != null)); 84 85 if (isLocal) { 86 result = Utils.makeDeepCopy(result); 87 } 88 if (!ciphered) { 91 long sessionID = 0; 92 93 try { 94 sessionID = psm.getSessionIDTo(destinationBody.getCertificate()); 95 96 if (sessionID == 0) { 97 psm.initiateSession(SecurityContext.COMMUNICATION_SEND_REPLY_TO, destinationBody); 98 sessionID = psm.getSessionIDTo(destinationBody.getCertificate()); 99 } 100 101 if (sessionID != 0) { 102 encryptedResult = psm.encrypt(sessionID, result); 103 104 ciphered = true; 105 this.sessionID = sessionID; 106 } 107 108 } catch (SecurityNotAvailableException e) { 110 } catch (Exception e) { 112 e.printStackTrace(); 113 } 114 } 115 116 destinationBody.receiveReply(this); 118 } 119 120 public boolean isCiphered() { 122 return ciphered; 123 } 124 125 public boolean decrypt(ProActiveSecurityManager psm) throws RenegotiateSessionException { 126 if ((sessionID != 0) && ciphered) { 127 byte[] decryptedMethodCall = psm.decrypt(sessionID, encryptedResult); 128 try { 129 ByteArrayInputStream bin = new ByteArrayInputStream (decryptedMethodCall); 130 ObjectInputStream in = new ObjectInputStream (bin); 131 result = (Object ) in.readObject(); 132 in.close(); 133 return true; 134 } catch (Exception e) { 135 e.printStackTrace(); 136 } 137 } 138 139 return false; 140 } 141 142 145 public long getSessionId() { 146 return sessionID; 147 } 148 } 149 | Popular Tags |