001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.panels.request.actions;
014:
015: import java.awt.Dimension;
016: import java.io.File;
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019: import java.net.URL;
020: import java.util.Calendar;
021:
022: import javax.swing.SwingUtilities;
023:
024: import org.wsI.testing.x2003.x03.common.AddStyleSheet;
025: import org.wsI.testing.x2003.x03.log.Environment;
026: import org.wsI.testing.x2003.x03.log.HttpMessageEntry;
027: import org.wsI.testing.x2003.x03.log.Implementation;
028: import org.wsI.testing.x2003.x03.log.Log;
029: import org.wsI.testing.x2003.x03.log.LogDocument;
030: import org.wsI.testing.x2003.x03.log.MessageEntry;
031: import org.wsI.testing.x2003.x03.log.Monitor;
032: import org.wsI.testing.x2003.x03.log.NameVersionPair;
033: import org.wsI.testing.x2003.x03.log.TcpMessageType;
034: import org.wsI.testing.x2004.x07.analyzerConfig.AssertionResults;
035: import org.wsI.testing.x2004.x07.analyzerConfig.Configuration;
036: import org.wsI.testing.x2004.x07.analyzerConfig.ConfigurationDocument;
037: import org.wsI.testing.x2004.x07.analyzerConfig.LogFile;
038: import org.wsI.testing.x2004.x07.analyzerConfig.ReportFile;
039: import org.wsI.testing.x2004.x07.analyzerConfig.LogFile.CorrelationType;
040:
041: import com.eviware.soapui.SoapUI;
042: import com.eviware.soapui.impl.wsdl.WsdlRequest;
043: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
044: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
045: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
046: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
047: import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
048: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsi.WSIAnalyzeAction;
049: import com.eviware.soapui.impl.wsdl.actions.iface.tools.wsi.WSIReportPanel;
050: import com.eviware.soapui.model.iface.Response;
051: import com.eviware.soapui.model.settings.Settings;
052: import com.eviware.soapui.settings.WSISettings;
053: import com.eviware.soapui.support.UISupport;
054: import com.eviware.soapui.support.types.StringToStringMap;
055: import com.eviware.soapui.ui.support.DefaultDesktopPanel;
056:
057: /**
058: * Validates the request XML of a WsdlRequest
059: *
060: * @author Ole.Matzura
061: */
062:
063: public class WSIValidateRequestAction extends
064: AbstractToolsAction<WsdlRequest> {
065: private String configFile;
066: private File logFile;
067:
068: public WSIValidateRequestAction() {
069: super ("Check WS-I Compliance",
070: "Validates the current request/response againt the WS-I Basic Profile");
071: }
072:
073: protected void generate(StringToStringMap values,
074: ToolHost toolHost, WsdlRequest modelItem) throws Exception {
075: if (modelItem.getResponse() == null) {
076: UISupport
077: .showErrorMessage("Response required for WS-I validations");
078: return;
079: }
080:
081: String wsiDir = SoapUI.getSettings().getString(
082: WSISettings.WSI_LOCATION, null);
083: if (wsiDir == null) {
084: UISupport
085: .showErrorMessage("WSI Test Tools directory must be set in global preferences");
086: return;
087: }
088:
089: if (modelItem.getAttachmentCount() > 0
090: || modelItem.getResponse().getAttachments().length > 0) {
091: if (!UISupport.confirm(
092: "Message contains attachments which is not supported by "
093: + "validation tools, validate anyway?",
094: "Validation Warning"))
095: return;
096: }
097:
098: ProcessBuilder builder = new ProcessBuilder();
099:
100: File reportFile = File.createTempFile("wsi-report", ".xml");
101:
102: ArgumentBuilder args = buildArgs(reportFile, modelItem);
103: builder.command(args.getArgs());
104: builder.directory(new File(wsiDir + File.separatorChar + "java"
105: + File.separatorChar + "bin"));
106:
107: toolHost.run(new WSIProcessToolRunner(builder, reportFile,
108: modelItem));
109: }
110:
111: private ArgumentBuilder buildArgs(File reportFile,
112: WsdlRequest modelItem) throws Exception {
113: File logFile = buildLog(modelItem);
114: File file = buildConfig(reportFile, logFile, modelItem);
115: Settings settings = modelItem.getSettings();
116:
117: ArgumentBuilder builder = new ArgumentBuilder(
118: new StringToStringMap());
119: builder.startScript("Analyzer", ".bat", ".sh");
120:
121: builder.addArgs("-config", file.getAbsolutePath());
122:
123: // add this to command-line due to bug in wsi-tools (?)
124: if (settings.getBoolean(WSISettings.ASSERTION_DESCRIPTION))
125: builder.addArgs("-assertionDescription", "true");
126:
127: return builder;
128: }
129:
130: private File buildLog(WsdlRequest modelItem) throws Exception {
131: LogDocument logDoc = LogDocument.Factory.newInstance();
132: Log log = logDoc.addNewLog();
133: log.setTimestamp(Calendar.getInstance());
134:
135: addMonitorConfig(log);
136: addMessageConfig(log, modelItem);
137:
138: logFile = File.createTempFile("wsi-analyzer-log", ".xml");
139: logDoc.save(logFile);
140: return logFile;
141: }
142:
143: private File buildConfig(File reportFile, File logFile,
144: WsdlRequest modelItem) throws IOException {
145: Settings settings = modelItem.getSettings();
146:
147: ConfigurationDocument configDoc = ConfigurationDocument.Factory
148: .newInstance();
149: Configuration config = configDoc.addNewConfiguration();
150:
151: config.setVerbose(settings.getBoolean(WSISettings.VERBOSE));
152: AssertionResults results = config.addNewAssertionResults();
153: results.setType(AssertionResults.Type.Enum.forString(settings
154: .getString(WSISettings.RESULTS_TYPE,
155: AssertionResults.Type.ONLY_FAILED.toString())));
156:
157: results.setMessageEntry(settings
158: .getBoolean(WSISettings.MESSAGE_ENTRY));
159: results.setFailureMessage(settings
160: .getBoolean(WSISettings.FAILURE_MESSAGE));
161: results.setAssertionDescription(settings
162: .getBoolean(WSISettings.ASSERTION_DESCRIPTION));
163:
164: ReportFile report = config.addNewReportFile();
165: report.setLocation(reportFile.getAbsolutePath());
166: report.setReplace(true);
167:
168: AddStyleSheet stylesheet = report.addNewAddStyleSheet();
169: stylesheet
170: .setHref(".\\..\\common\\Profiles\\SSBP10_BP11_TAD.xml");
171: stylesheet.setType("text/xsl");
172: stylesheet.setAlternate(false);
173:
174: config
175: .setTestAssertionsFile("../../common/profiles/SSBP10_BP11_TAD.xml");
176:
177: LogFile logFileConfig = config.addNewLogFile();
178: logFileConfig.setStringValue(logFile.getAbsolutePath());
179: logFileConfig.setCorrelationType(CorrelationType.ENDPOINT);
180:
181: /*
182: WsdlInterface iface = (WsdlInterface) modelItem.getOperation().getInterface();
183:
184: WsdlReferenceConfig wsdlRef = config.addNewWsdlReference();
185: wsdlRef.setWsdlURI( iface.getDefinition() );
186: WsdlElementReferenceConfig wsdlElement = wsdlRef.addNewWsdlElement();
187: wsdlElement.setType( WsdlElementTypeConfig.BINDING );
188: wsdlElement.setStringValue( iface.getBindingName().getLocalPart() );
189: wsdlElement.setNamespace( iface.getBindingName().getNamespaceURI() );
190: wsdlRef.setServiceLocation( modelItem.getEndpoint() );
191: */
192:
193: configFile = configDoc.toString();
194:
195: File file = File.createTempFile("wsi-analyzer-config", ".xml");
196:
197: configDoc.save(file);
198: return file;
199: }
200:
201: private void addMessageConfig(Log log, WsdlRequest modelItem)
202: throws MalformedURLException {
203: HttpMessageEntry requestMessage = HttpMessageEntry.Factory
204: .newInstance();
205: requestMessage.addNewMessageContent().setStringValue(
206: modelItem.getRequestContent());
207: requestMessage.setConversationID("1");
208: requestMessage.setTimestamp(Calendar.getInstance());
209: requestMessage.setID("1");
210: URL endpoint = new URL(modelItem.getEndpoint());
211: requestMessage.setSenderHostAndPort("localhost");
212:
213: if (endpoint.getPort() > 0)
214: requestMessage.setReceiverHostAndPort(endpoint.getHost()
215: + ":" + endpoint.getPort());
216: else
217: requestMessage.setReceiverHostAndPort(endpoint.getHost());
218:
219: requestMessage.setType(TcpMessageType.REQUEST);
220:
221: Response response = modelItem.getResponse();
222: HttpMessageEntry responseMessage = HttpMessageEntry.Factory
223: .newInstance();
224: responseMessage.addNewMessageContent().setStringValue(
225: response.getContentAsString());
226: responseMessage.setConversationID("1");
227: responseMessage.setType(TcpMessageType.RESPONSE);
228: responseMessage.setTimestamp(Calendar.getInstance());
229: responseMessage.setID("2");
230: responseMessage.setSenderHostAndPort(requestMessage
231: .getReceiverHostAndPort());
232: responseMessage.setReceiverHostAndPort(requestMessage
233: .getSenderHostAndPort());
234:
235: String requestHeaders = buildHttpHeadersString(response
236: .getRequestHeaders());
237: requestMessage.setHttpHeaders("POST " + endpoint.getPath()
238: + " HTTP/1.1\r\n" + requestHeaders);
239:
240: responseMessage.setHttpHeaders(buildHttpHeadersString(response
241: .getResponseHeaders()));
242:
243: log.setMessageEntryArray(new MessageEntry[] { requestMessage,
244: responseMessage });
245: }
246:
247: private void addMonitorConfig(Log log) throws Exception {
248: Monitor monitor = log.addNewMonitor();
249:
250: monitor.setVersion("1.5");
251: monitor.setReleaseDate(Calendar.getInstance());
252:
253: org.wsI.testing.x2003.x03.monitorConfig.Configuration conf = monitor
254: .addNewConfiguration();
255: conf.setCleanupTimeoutSeconds(0);
256: conf.setLogDuration(0);
257:
258: org.wsI.testing.x2003.x03.monitorConfig.LogFile logFileConf = conf
259: .addNewLogFile();
260: logFileConf.setLocation("report.xml");
261: logFileConf.setReplace(true);
262:
263: /*
264: ArrayOfRedirectConfig mintConf = conf.addNewManInTheMiddle();
265: RedirectConfig redirect = mintConf.addNewRedirect();
266: redirect.setListenPort( 9999 );
267: redirect.setMaxConnections( 10 );
268: redirect.setReadTimeoutSeconds( 10 );
269:
270: URL endpoint = new URL( modelItem.getEndpoint());
271: if( endpoint.getPort() > 0 )
272: redirect.setSchemeAndHostPort( endpoint.getHost() + ":" + endpoint.getPort());
273: else
274: redirect.setSchemeAndHostPort( endpoint.getHost() );
275: */
276:
277: Environment env = monitor.addNewEnvironment();
278: NameVersionPair osConf = env.addNewOperatingSystem();
279: osConf.setName("Windows");
280: osConf.setVersion("2003");
281:
282: NameVersionPair rtConf = env.addNewRuntime();
283: rtConf.setName("java");
284: rtConf.setVersion("1.5");
285:
286: NameVersionPair xpConf = env.addNewXmlParser();
287: xpConf.setName("xmlbeans");
288: xpConf.setVersion("2.1.0");
289:
290: Implementation implConf = monitor.addNewImplementer();
291: implConf.setName("soapui");
292: implConf.setLocation("here");
293: }
294:
295: private String buildHttpHeadersString(
296: StringToStringMap requestHeaders) {
297: StringBuffer buffer = new StringBuffer();
298:
299: if (requestHeaders.containsKey("#status#")) {
300: buffer.append(requestHeaders.get("#status#"))
301: .append("\r\n");
302: }
303:
304: for (String header : requestHeaders.keySet()) {
305: if (!header.equals("#status#"))
306: buffer.append(header).append(": ").append(
307: requestHeaders.get(header)).append("\r\n");
308: }
309:
310: return buffer.toString();
311: }
312:
313: private class WSIProcessToolRunner extends ProcessToolRunner {
314: private final File reportFile;
315: private final WsdlRequest modelItem;
316:
317: public WSIProcessToolRunner(ProcessBuilder builder,
318: File reportFile, WsdlRequest modelItem) {
319: super (builder, "WSI Message Validation", modelItem);
320: this .reportFile = reportFile;
321: this .modelItem = modelItem;
322: }
323:
324: public String getDescription() {
325: return "Running WSI Analysis tools..";
326: }
327:
328: protected void afterRun(int exitCode, RunnerContext context) {
329: if (exitCode == 0
330: && context.getStatus() == RunnerContext.RunnerStatus.FINISHED) {
331: SwingUtilities.invokeLater(new Runnable() {
332:
333: public void run() {
334: try {
335: WSIReportPanel panel = new WSIReportPanel(
336: WSIAnalyzeAction
337: .transformReport(reportFile),
338: configFile, logFile);
339: panel.setPreferredSize(new Dimension(600,
340: 400));
341:
342: UISupport
343: .showDesktopPanel(new DefaultDesktopPanel(
344: "WS-I Report",
345: "WS-I Report for validation of messages in request ["
346: + modelItem
347: .getName()
348: + "]", panel));
349: } catch (Exception e) {
350: UISupport.showErrorMessage(e);
351: }
352: }
353: });
354: }
355: }
356:
357: public boolean showLog() {
358: return modelItem.getSettings().getBoolean(
359: WSISettings.SHOW_LOG);
360: }
361: }
362: }
|