001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.request;
015:
016: import org.itsnat.core.ItsNatException;
017: import org.itsnat.core.event.RemoteControlEvent;
018: import org.itsnat.impl.core.*;
019: import org.itsnat.impl.core.client.ClientDocumentInvitedRemoteCtrlCometImpl;
020: import org.itsnat.impl.core.client.ClientDocumentInvitedRemoteCtrlImpl;
021: import org.itsnat.impl.core.client.ClientDocumentInvitedRemoteCtrlTimerImpl;
022: import org.itsnat.impl.core.event.client2serv.RemoteControlCometTaskEventImpl;
023: import org.itsnat.impl.core.event.client2serv.RemoteControlEventImpl;
024: import org.itsnat.impl.core.event.client2serv.RemoteControlTimerEventImpl;
025: import org.itsnat.impl.core.response.ResponseRemoteControlLoadDocImpl;
026:
027: /**
028: *
029: * @author jmarranz
030: */
031: public class RequestRemoteControlLoadDocImpl extends
032: RequestRemoteControlImpl {
033:
034: /**
035: * Creates a new instance of RequestRemoteControlLoadDocImpl
036: */
037: public RequestRemoteControlLoadDocImpl(
038: ItsNatServletRequestImpl itsNatRequest) {
039: super (itsNatRequest);
040: }
041:
042: public void process() {
043: // La sesión que se monitoriza no tiene porqué ser la misma del monitorizador
044: String sessionId = getParameter("itsnat_session_id"); // Debe existir (sino da error)
045: ItsNatServletContextImpl context = itsNatRequest
046: .getItsNatServletContext();
047: ItsNatSessionImpl remSession = context
048: .findItsNatSessionById(sessionId);
049: if (remSession == null)
050: throw new ItsNatException("Remote session with id "
051: + sessionId + " does not exist");
052:
053: ItsNatSessionImpl session = itsNatRequest
054: .getItsNatSessionImpl();
055: if (!remSession.isUserAgentCompatible(session.getUserAgent()))
056: throw new ItsNatException(
057: "User agent is not compatible with the remote user agent: "
058: + remSession.getUserAgent());
059:
060: // El documento a monitorizar está registrado en la sesión remota:
061: String docId = getParameter("itsnat_doc_id"); // Debe existir (sino da error)
062: ItsNatDocumentImpl itsNatDoc = remSession
063: .getItsNatDocumentImplById(docId);
064: if (itsNatDoc == null)
065: throw new ItsNatException(
066: "Observed session/document with id "
067: + remSession.getId() + "/" + docId
068: + " does not exist");
069:
070: synchronized (itsNatDoc) // Sólo un hilo puede acceder al mismo documento
071: {
072: itsNatRequest.setItsNatDocument(itsNatDoc);
073:
074: try {
075: // Evaluamos si está autorizado para controlar remótamente
076: int syncMode;
077: String syncModeStr = getParameter("itsnat_syncmode",
078: false); // Opcional
079: if (syncModeStr != null)
080: syncMode = Integer.parseInt(syncModeStr);
081: else
082: syncMode = itsNatDoc.getDefaultSyncMode();
083:
084: long ajaxTimeout;
085: String ajaxTimeoutStr = getParameter(
086: "itsnat_ajaxtimeout", false); // Opcional
087: if (ajaxTimeoutStr != null)
088: ajaxTimeout = Long.parseLong(ajaxTimeoutStr);
089: else
090: ajaxTimeout = itsNatDoc.getAJAXTimeout();
091:
092: RemoteControlEventImpl requestEvent = null;
093:
094: // Phase RemoteControlEvent.REQUEST;
095: String refreshMethod = getParameter("itsnat_refresh_method");
096: if (refreshMethod.equals("timer")) {
097: int interval = Integer
098: .parseInt(getParameter("itsnat_refresh_interval"));
099:
100: requestEvent = new RemoteControlTimerEventImpl(
101: interval, syncMode, ajaxTimeout,
102: itsNatRequest);
103: } else if (refreshMethod.equals("comet")) {
104: requestEvent = new RemoteControlCometTaskEventImpl(
105: syncMode, ajaxTimeout, itsNatRequest);
106: } else
107: throw new ItsNatException(
108: "Unrecognized refresh method: \""
109: + refreshMethod + "\"");
110:
111: itsNatDoc.dispatchRemoteControlListeners(requestEvent);
112: if (!requestEvent.isAccepted())
113: throw new ItsNatException(
114: "Request to remote control the document/page \""
115: + itsNatDoc
116: .getDocumentTemplateImpl()
117: .getName()
118: + "\" is not accepted");
119:
120: int phase = RemoteControlEvent.LOAD;
121:
122: ClientDocumentInvitedRemoteCtrlImpl observer = null;
123: if (requestEvent instanceof RemoteControlTimerEventImpl) {
124: RemoteControlTimerEventImpl requestTimerEvent = (RemoteControlTimerEventImpl) requestEvent;
125: observer = new ClientDocumentInvitedRemoteCtrlTimerImpl(
126: requestTimerEvent.getRefreshInterval(),
127: phase, requestEvent.getSyncMode(),
128: requestEvent.getAJAXTimeout(), session,
129: itsNatDoc);
130: } else if (requestEvent instanceof RemoteControlCometTaskEventImpl) {
131: observer = new ClientDocumentInvitedRemoteCtrlCometImpl(
132: phase, requestEvent.getSyncMode(),
133: requestEvent.getAJAXTimeout(), session,
134: itsNatDoc);
135: }
136:
137: itsNatRequest.setClientDocument(observer);
138: session.addClientDocumentInvitedRemoteCtrl(observer); // Sujeta el objeto
139: itsNatDoc.addClientDocumentInvitedRemoteCtrl(observer); // permite que se notifiquen los cambios
140:
141: ResponseRemoteControlLoadDocImpl response = new ResponseRemoteControlLoadDocImpl(
142: requestEvent, itsNatRequest
143: .getItsNatServletResponseImpl());
144: try {
145: response.process();
146:
147: if (observer.getPhase() == RemoteControlEvent.LOAD)
148: observer.setPhase(RemoteControlEvent.REFRESH);
149: } catch (Exception ex) {
150: session
151: .removeClientDocumentInvitedRemoteCtrl(observer);
152: itsNatDoc
153: .removeClientDocumentInvitedRemoteCtrl(observer); // El documento no lo retiene pero así se quita explícitamente
154:
155: throw new ItsNatException(ex);
156: }
157: } finally {
158: itsNatRequest.unsetItsNatDocument();
159: itsNatRequest.setClientDocument(null);
160: }
161: }
162: }
163: }
|