View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.authortool.ws;
5   
6   import java.io.File;
7   import java.io.FileOutputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.MalformedURLException;
11  import java.net.URL;
12  
13  import javax.activation.DataHandler;
14  import javax.swing.JOptionPane;
15  
16  import net.sf.sail.core.curnit.Curnit;
17  import net.sf.sail.core.curnit.CurnitArchiveResolver;
18  import net.sf.sail.core.curnit.CurnitFile;
19  import net.sf.sail.core.uuid.CurnitUuid;
20  
21  import org.apache.commons.io.IOUtils;
22  import org.codehaus.xfire.attachments.StreamedAttachments;
23  import org.codehaus.xfire.client.XFireProxyFactory;
24  import org.codehaus.xfire.service.Service;
25  import org.codehaus.xfire.service.binding.ObjectServiceFactory;
26  import org.doomdark.uuid.UUID;
27  import org.telscenter.pas.authortool.context.IAuthoringSessionManager;
28  import org.telscenter.pas.remote.AuthoringSessionDescriptor;
29  import org.telscenter.pas.remote.RemoteAuthoring;
30  
31  /***
32   * @author turadg
33   * 
34   */
35  public class WebServiceSessionManager implements IAuthoringSessionManager {
36  
37  	private AuthoringSessionDescriptor sessionDescriptor;
38  
39  	private RemoteAuthoring remoteAuthoringService;
40  
41  	/***
42  	 * @param token
43  	 */
44  	public WebServiceSessionManager(URL remoteAuthoringServiceUrl,
45  			UUID authoringSessionToken) {
46  		final ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
47  		final Service serviceModel = serviceFactory
48  				.create(RemoteAuthoring.class);
49  		serviceModel.setProperty("mtom-enabled", "true");
50  		serviceModel.setProperty(
51  				StreamedAttachments.ATTACHMENT_MEMORY_THRESHOLD, new Integer(
52  						10 * 1024 * 1024));
53  
54  		// Create a client proxy
55  		final XFireProxyFactory proxyFactory = new XFireProxyFactory();
56  		try {
57  			remoteAuthoringService = (RemoteAuthoring) proxyFactory.create(
58  					serviceModel, remoteAuthoringServiceUrl.toExternalForm());
59  		} catch (MalformedURLException e) {
60  			// impossible, since it was a URL to begin with
61  			throw new Error(e);
62  		}
63  
64  		sessionDescriptor = remoteAuthoringService
65  				.getSessionDescriptor(authoringSessionToken.toString());
66  	}
67  
68  	public Curnit getCurnit() {
69  		try {
70  			File tempFile = File.createTempFile("tempCurnitFileForEditing",
71  					".zip");
72  			FileOutputStream fos = new FileOutputStream(tempFile);
73  
74  			InputStream curnitStream = getCurnitInputStream();
75  			IOUtils.copy(curnitStream, fos);
76  			fos.close();
77  
78  			CurnitFile curnitFile = new CurnitFile(tempFile);
79  			Curnit curnit = curnitFile.getCurnit();
80  
81  			if (curnit == null) {
82  				// FIXME throw exception
83  				return null;
84  			}
85  			
86  			// register curnit archive with resolver
87  			CurnitUuid curnitId = curnit.getCurnitId();
88  			CurnitArchiveResolver.getSystemResolver().put(curnitId, tempFile.toURL());
89  
90  			return curnit;
91  		} catch (IOException e) {
92  			// TODO Auto-generated catch block
93  			e.printStackTrace();
94  		}
95  		return null;
96  	}
97  
98  	/***
99  	 * @return
100 	 * @throws IOException
101 	 */
102 	private InputStream getCurnitInputStream() throws IOException {
103 		String authoringSessionTokenUuidStr = getSessionTokenStr();
104 
105 		DataHandler curnitArchiveData = remoteAuthoringService
106 				.getCurnitArchiveData(authoringSessionTokenUuidStr);
107 		return curnitArchiveData.getInputStream();
108 	}
109 
110 	/***
111 	 * @return
112 	 */
113 	private String getSessionTokenStr() {
114 		String authoringSessionTokenUuidStr = sessionDescriptor.getSessionUuidStr();
115 		if (authoringSessionTokenUuidStr == null)
116 			throw new IllegalStateException("no token in session");
117 		return authoringSessionTokenUuidStr;
118 	}
119 
120 	public boolean requestExtension(int minutes) {
121 		// TODO Auto-generated method stub
122 		return false;
123 	}
124 
125 	public void save(Curnit curnit) {
126 		JOptionPane.showMessageDialog(null, "Save not yet implemented");
127 	}
128 
129 	public void saveAs(Curnit arg0) throws IOException {
130 		JOptionPane.showMessageDialog(null, "Save As not yet implemented");
131 	}
132 
133 	public void endAuthoringSession() {
134 		String authoringSessionTokenUuidStr = getSessionTokenStr();
135 		remoteAuthoringService.endAuthoringSession(authoringSessionTokenUuidStr);
136 	}
137 
138 	public void startAuthoringSession() {
139 		String authoringSessionTokenUuidStr = getSessionTokenStr();
140 		remoteAuthoringService.startAuthoringSession(authoringSessionTokenUuidStr);
141 	}
142 
143 }