001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.service;
016:
017: import java.io.ByteArrayOutputStream;
018: import java.io.OutputStream;
019: import javax.servlet.http.HttpServletResponse;
020: import org.araneaframework.Environment;
021: import org.araneaframework.InputData;
022: import org.araneaframework.OutputData;
023: import org.araneaframework.Path;
024: import org.araneaframework.core.BaseApplicationWidget;
025: import org.araneaframework.core.BaseService;
026: import org.araneaframework.framework.ManagedServiceContext;
027: import org.araneaframework.http.HttpInputData;
028: import org.araneaframework.http.HttpOutputData;
029: import org.araneaframework.http.PopupWindowContext;
030: import org.araneaframework.http.filter.StandardPopupFilterWidget.StandardPopupServiceInfo;
031: import org.araneaframework.http.util.EnvironmentUtil;
032: import org.araneaframework.http.util.FileImportUtil;
033: import org.araneaframework.http.util.ServletUtil;
034:
035: /**
036: * Service that returns response that closes browser window that made the
037: * request; and if possible, reloads the opener of that window.
038: *
039: * @author Taimo Peelo (taimo@araneaframework.org)
040: */
041: public class WindowClosingService extends BaseService {
042: private Environment closableComponentEnv;
043:
044: public WindowClosingService(Environment closableComponentEnv) {
045: this .closableComponentEnv = closableComponentEnv;
046: }
047:
048: protected void action(Path path, InputData input, OutputData output)
049: throws Exception {
050: HttpServletResponse response = ServletUtil.getResponse(output);
051:
052: PopupWindowContext popupCtx = ((PopupWindowContext) closableComponentEnv
053: .getEntry(PopupWindowContext.class));
054: BaseApplicationWidget opener = null;
055: if (popupCtx != null)
056: opener = (BaseApplicationWidget) popupCtx.getOpener();
057:
058: StandardPopupServiceInfo serviceInfo = null;
059: if (opener != null) {
060: String threadId = (String) EnvironmentUtil
061: .requireThreadServiceId(opener.getEnvironment());
062: String topserviceId = (String) EnvironmentUtil
063: .requireTopServiceId(opener.getEnvironment());
064: String url = ((HttpOutputData) getInputData()
065: .getOutputData())
066: .encodeURL(((HttpInputData) getInputData())
067: .getContainerURL());
068: serviceInfo = new StandardPopupServiceInfo(topserviceId,
069: threadId, null, url);
070: serviceInfo.setTransactionOverride(false);
071: }
072:
073: String script;
074:
075: if (serviceInfo != null) {
076: script = "Aranea.Popups.reloadParentWindow('"
077: + serviceInfo.toURL() + "');"
078: + "Aranea.Popups.delayedCloseWindow(50);";
079: } else {
080: script = "Aranea.Popups.delayedCloseWindow(50);";
081: }
082:
083: String scriptSrc = FileImportUtil.getImportString(
084: "js/aranea/aranea-popups.js", input);
085:
086: String responseStr = "<html>" + "<head>"
087: + "<script type=\"text/javascript\" src=\"" + scriptSrc
088: + "\"></script>" + "</head>" + "<body onload=\""
089: + script + "\">" + "</body>" + "</html>";
090:
091: byte[] rsp = responseStr.getBytes();
092:
093: ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
094: byteOutputStream.write(rsp);
095:
096: response.setContentType("text/html");
097: response.setContentLength(byteOutputStream.size());
098:
099: OutputStream out = response.getOutputStream();
100: byteOutputStream.writeTo(out);
101: out.flush();
102:
103: ManagedServiceContext mngCtx = (ManagedServiceContext) getEnvironment()
104: .getEntry(ManagedServiceContext.class);
105: mngCtx.close(mngCtx.getCurrentId());
106: }
107: }
|