001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.websvc.core.jaxws.actions;
042:
043: import java.io.IOException;
044: import java.net.HttpURLConnection;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.net.URLConnection;
048: import java.text.MessageFormat;
049: import java.util.Map;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
053: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
054: import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
055: import org.netbeans.modules.websvc.jaxws.api.JaxWsTesterCookie;
056: import org.openide.DialogDisplayer;
057: import org.openide.ErrorManager;
058: import org.openide.NotifyDescriptor;
059: import org.openide.awt.HtmlBrowser;
060: import org.openide.awt.StatusDisplayer;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.RequestProcessor;
063: import org.openide.util.actions.CookieAction;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.NbBundle;
066: import org.openide.nodes.Node;
067:
068: public class WsTesterPageAction extends CookieAction {
069: public String getName() {
070: return NbBundle.getMessage(WsTesterPageAction.class,
071: "LBL_TesterPageAction");
072: }
073:
074: public HelpCtx getHelpCtx() {
075: return HelpCtx.DEFAULT_HELP;
076: }
077:
078: protected int mode() {
079: return MODE_EXACTLY_ONE;
080: }
081:
082: protected Class[] cookieClasses() {
083: return new Class[] { JaxWsTesterCookie.class };
084: }
085:
086: protected boolean asynchronous() {
087: return false;
088: }
089:
090: protected void performAction(Node[] activatedNodes) {
091: JaxWsTesterCookie cookie = activatedNodes[0]
092: .getCookie(JaxWsTesterCookie.class);
093: String wsdlURL = cookie.getTesterPageURL();
094: try {
095: final URL url = new URL(wsdlURL);
096: if (url != null) {
097: RequestProcessor.getDefault().post(new Runnable() {
098: public void run() {
099: boolean connectionOK = false;
100: try {
101: URLConnection connection = url
102: .openConnection();
103: if (connection instanceof HttpURLConnection) {
104: HttpURLConnection httpConnection = (HttpURLConnection) connection;
105: try {
106: httpConnection
107: .setRequestMethod("GET"); //NOI18N
108: httpConnection.connect();
109: int responseCode = httpConnection
110: .getResponseCode();
111: // for secured web services the response code is 405: we should allow to show the response
112: if (HttpURLConnection.HTTP_OK == responseCode
113: || HttpURLConnection.HTTP_BAD_METHOD == responseCode)
114: connectionOK = true;
115: } catch (java.net.ConnectException ex) {
116: // doing nothing - display warning
117: } finally {
118: if (httpConnection != null)
119: httpConnection.disconnect();
120: }
121: }
122:
123: } catch (IOException ex) {
124: ErrorManager.getDefault().notify(ex);
125: }
126: if (connectionOK) {
127: HtmlBrowser.URLDisplayer.getDefault()
128: .showURL(url);
129: } else {
130: DialogDisplayer
131: .getDefault()
132: .notify(
133: new NotifyDescriptor.Message(
134: NbBundle
135: .getMessage(
136: WsTesterPageAction.class,
137: "MSG_UNABLE_TO_OPEN_TEST_PAGE",
138: url),
139: NotifyDescriptor.WARNING_MESSAGE));
140: }
141: }
142:
143: });
144: }
145: } catch (MalformedURLException ex) {
146: StatusDisplayer.getDefault().setStatusText(
147: NbBundle.getMessage(WsTesterPageAction.class,
148: "TXT_TesterPageUrl", wsdlURL)); //NOI18N
149: }
150: }
151:
152: protected boolean enable(Node[] activatedNodes) {
153: if (activatedNodes == null || activatedNodes.length == 0)
154: return false;
155: FileObject srcRoot = (FileObject) activatedNodes[0].getLookup()
156: .lookup(FileObject.class);
157: if (srcRoot != null) {
158: Project project = FileOwnerQuery.getOwner(srcRoot);
159: if (project != null) {
160: return isTesterPageSupported(project);
161: }
162: }
163: return false;
164: }
165:
166: private boolean isTesterPageSupported(Project project) {
167: JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(project
168: .getProjectDirectory());
169: if (wss != null) {
170: Map properties = wss.getAntProjectHelper()
171: .getStandardPropertyEvaluator().getProperties();
172: String serverInstance = (String) properties
173: .get("j2ee.server.instance"); //NOI18N
174: if (serverInstance != null) {
175: J2eePlatform j2eePlatform = Deployment.getDefault()
176: .getJ2eePlatform(serverInstance);
177: if (j2eePlatform != null) {
178: return j2eePlatform.isToolSupported("jaxws-tester"); //NOI18N
179: }
180: }
181: }
182: return false;
183: }
184:
185: }
|