Skip to content

Commit

Permalink
Improve IA generator module
Browse files Browse the repository at this point in the history
- Parameter names of web service methods should be camelcase
- Add a default logger to the service template
- Redirect log output to System.out (with debug level)
- Add shared utility class for easily access SOAP headers
- Move AbstractIAService.java to shared classes

Signed-off-by: Michael Wurster <miwurster@googlemail.com>
  • Loading branch information
miwurster committed Sep 20, 2018
1 parent 7c84140 commit 6a2507b
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 233 deletions.
20 changes: 15 additions & 5 deletions org.eclipse.winery.generators.ia/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@
<groupId>org.eclipse.winery</groupId>
<artifactId>org.eclipse.winery.common</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.winery</groupId>
<artifactId>org.eclipse.winery.highlevelrestapi</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.winery</groupId>
<artifactId>org.eclipse.winery.model.tosca</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.6</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.tukaani</groupId>
Expand All @@ -66,7 +67,16 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,38 @@
*******************************************************************************/
package org.eclipse.winery.generators.ia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.eclipse.winery.model.tosca.TBoolean;
import org.eclipse.winery.model.tosca.TInterface;
import org.eclipse.winery.model.tosca.TOperation;
import org.eclipse.winery.model.tosca.TParameter;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.eclipse.winery.model.tosca.TBoolean;
import org.eclipse.winery.model.tosca.TInterface;
import org.eclipse.winery.model.tosca.TOperation;
import org.eclipse.winery.model.tosca.TParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Generator {

private static final Logger LOGGER = LoggerFactory.getLogger(Generator.class);
Expand Down Expand Up @@ -64,7 +73,6 @@ public class Generator {
private final String namespace;
private final URL iaArtifactTemplateUploadUrl;


/**
* Creates a new IA Generator instance for the given {@link TInterface}.
*
Expand Down Expand Up @@ -177,12 +185,6 @@ public Path generateProject() throws Exception {
javaFolderString += File.separator + splitPkg[i];
}

// Copy TEMPLATE_JAVA_ABSTRACT_IA_SERVICE
Path templateAbstractIAService = javaTemplateDir.resolve(Generator.TEMPLATE_JAVA_ABSTRACT_IA_SERVICE);
File javaAbstractIAService = new File(javaFolderString + File.separator + "AbstractIAService.java");
Files.createDirectories(javaAbstractIAService.toPath().getParent());
Files.copy(templateAbstractIAService, javaAbstractIAService.toPath(), StandardCopyOption.REPLACE_EXISTING);

// Copy and rename TEMPLATE_JAVA_TEMPLATE_SERVICE
Path templateJavaService = javaTemplateDir.resolve(Generator.TEMPLATE_JAVA_TEMPLATE_SERVICE);
File javaService = new File(javaFolderString + File.separator + this.name + ".java");
Expand Down Expand Up @@ -236,14 +238,7 @@ private void generateJavaFile(File javaService) throws IOException {
// Generate @WebParam
sb.append("@WebParam(name=\"" + parameterName + "\", targetNamespace=\"" + this.namespace + "\") ");

// Handle required and optional parameters using @XmlElement
if (parameter.getRequired().equals(TBoolean.YES)) {
sb.append("@XmlElement(required=true)");
} else {
sb.append("@XmlElement(required=false)");
}

sb.append(" String " + parameterName);
sb.append("String " + parameterName);
}
}
sb.append("\n\t) {\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/

package org.eclipse.winery.generators.ia.jaxws;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

import org.eclipse.winery.highlevelrestapi.HighLevelRestApi;

import org.apache.cxf.headers.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

/**
* Abstract class providing common functionality for JAX-WS web services.
*/
public abstract class AbstractService {

private static final Logger logger = LoggerFactory.getLogger(AbstractService.class);

@Resource
protected WebServiceContext ctx;

protected void sendResponse(HashMap<String, String> returnParameters) {

// Extract headers from message
List<Header> headers = Headers.asList(ctx);

// Find ReplyTo and MessageID SOAP Header
String replyTo = null;
String messageID = null;
for (Header iter : headers) {
Object headerObject = iter.getObject();
// Unmarshall to org.w3c.dom.Node
if (headerObject instanceof Node) {
Node node = (Node) headerObject;
String localPart = iter.getName().getLocalPart();
String content = node.getTextContent();
// Extract ReplyTo Header value
if ("ReplyTo".equals(localPart)) {
replyTo = content;
}
// Extract MessageID Header value
if ("MessageID".equals(localPart)) {
messageID = content;
}
}
}

// Create asynchronous SOAP Response Message
StringBuilder builder = new StringBuilder();

builder.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sch='http://siserver.org/schema'>");
builder.append(" <soapenv:Header/>");
builder.append(" <soapenv:Body>");
builder.append(" <sch:invokeResponse>");
builder.append(" <sch:MessageID>" + messageID + "</sch:MessageID>");

// Insert return parameters into asynchronous SOAP Response Message
for (Entry<String, String> paramIter : returnParameters.entrySet()) {
String key = paramIter.getKey();
String value = paramIter.getValue();
builder.append(" <" + key + ">" + value + "</" + key + ">");
}

builder.append(" </sch:invokeResponse>");
builder.append(" </soapenv:Body>");
builder.append("</soapenv:Envelope>");

// Send SOAP Response Message back to requester
if (replyTo == null) {
logger.warn("No 'ReplyTo' header found! Therefore, print reply message:\n{}", builder.toString());
} else {
HighLevelRestApi.Post(replyTo, builder.toString(), "");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/

package org.eclipse.winery.generators.ia.jaxws;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.jaxws.context.WrappedMessageContext;
import org.apache.cxf.message.Message;
import org.w3c.dom.Element;

/**
* Helper class to provide easy access to SOAP headers.
*/
public final class Headers {

public static Map<String, String> asMap(final WebServiceContext ctx) {
Map<String, String> headers = new HashMap<>();
for (Header h : asList(ctx)) {
Element n = (Element) h.getObject();
headers.put(n.getLocalName(), n.getTextContent());
}
return headers;
}

public static List<Header> asList(final WebServiceContext ctx) {
MessageContext messageContext = ctx.getMessageContext();
if (messageContext instanceof WrappedMessageContext) {
Message message = ((WrappedMessageContext) messageContext).getWrappedMessage();
return CastUtils.cast((List<?>) message.get(Header.HEADER_LIST));
}
return new ArrayList<>();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.winery.generators.ia.jaxws.AbstractService;

@WebService
public class IA_CLASS_NAME extends AbstractIAService {
public class IA_CLASS_NAME extends AbstractService {

private static final Logger logger = LoggerFactory.getLogger(
IA_CLASS_NAME.class
);

GENERATED_WEBSERVICE_METHODS

}
}
Loading

0 comments on commit 6a2507b

Please sign in to comment.