Skip to content

Commit

Permalink
First attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
FloBoJa committed Mar 2, 2024
1 parent a72856b commit 2741e20
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.palladiosimulator.retriever.extraction.commonalities.CompUnitOrName
import org.palladiosimulator.retriever.extraction.engine.Rule
import org.palladiosimulator.retriever.extraction.rules.data.GatewayRoute
import org.palladiosimulator.retriever.extraction.blackboard.RetrieverBlackboard
import org.palladiosimulator.retriever.extraction.commonalities.HTTPMethod
import org.palladiosimulator.retriever.extraction.commonalities.RESTOperationName

class EcmaScriptRules implements Rule {

Expand Down Expand Up @@ -86,7 +86,7 @@ class EcmaScriptRules implements Rule {
pcmDetector.detectRequiredInterface(GATEWAY_NAME, mappedURL)
}
pcmDetector.detectProvidedOperation(GATEWAY_NAME, null,
new RESTName(hostname, "/" + url, HTTPMethod.any))
new RESTOperationName(hostname, "/" + url))
}
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ class EcmaScriptRules implements Rule {
return route.applyTo(url)
}
}
return new RESTName(host, url, HTTPMethod.any)
return new RESTName(host, url)
}

override isBuildRule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import static org.palladiosimulator.retriever.extraction.engine.RuleHelper.*
import org.palladiosimulator.retriever.extraction.commonalities.CompUnitOrName
import java.util.Set
import org.palladiosimulator.retriever.extraction.engine.Rule
import org.palladiosimulator.retriever.extraction.commonalities.RESTName
import org.palladiosimulator.retriever.extraction.rules.util.RESTHelper
import java.util.Map
import org.palladiosimulator.retriever.extraction.commonalities.HTTPMethod
import org.palladiosimulator.retriever.extraction.blackboard.RetrieverBlackboard
import org.palladiosimulator.retriever.extraction.commonalities.RESTOperationName

class JaxRSRules implements Rule {

Expand Down Expand Up @@ -67,7 +67,7 @@ class JaxRSRules implements Rule {
methodPath = RESTHelper.replaceArgumentsWithWildcards(methodPath)
// TODO: HTTP method switch-case
pcmDetector.detectCompositeProvidedOperation(identifier, m.resolveBinding,
new RESTName("SERVICE-HOST", methodPath, HTTPMethod.all))
new RESTOperationName("SERVICE-HOST", methodPath))
}
]
getFields(unit).forEach[f|pcmDetector.detectRequiredInterfaceWeakly(identifier, f)]
Expand All @@ -82,7 +82,7 @@ class JaxRSRules implements Rule {
getMethods(unit).forEach [ m |
if (SERVLET_METHODS.containsKey(m.name.identifier)) {
pcmDetector.detectProvidedOperation(identifier, m.resolveBinding,
new RESTName("SERVICE-HOST", path, Set.of(SERVLET_METHODS.get(m.name.identifier))))
new RESTOperationName("SERVICE-HOST", path, Set.of(SERVLET_METHODS.get(m.name.identifier))))
}
]
getFields(unit).forEach[f|pcmDetector.detectRequiredInterfaceWeakly(identifier, f)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.palladiosimulator.retriever.extraction.engine.Rule
import org.palladiosimulator.retriever.extraction.rules.util.SpringHelper
import org.palladiosimulator.retriever.extraction.rules.util.RESTHelper
import org.palladiosimulator.retriever.extraction.blackboard.RetrieverBlackboard
import org.palladiosimulator.retriever.extraction.commonalities.RESTOperationName

class SpringRules implements Rule {
static final Logger LOG = Logger.getLogger(SpringRules)
Expand Down Expand Up @@ -188,7 +189,7 @@ class SpringRules implements Rule {
methodName = RESTHelper.replaceArgumentsWithWildcards(methodName);
val httpMethod = getHTTPMethod(m);
pcmDetector.detectCompositeProvidedOperation(identifier, m.resolveBinding,
new RESTName(applicationName, methodName, httpMethod));
new RESTOperationName(applicationName, methodName, httpMethod));
}
}
}
Expand All @@ -210,9 +211,10 @@ class SpringRules implements Rule {
requestedMapping = substituteVariables(requestedMapping, contextVariables);
var methodName = ifaceName + "/" + requestedMapping;
methodName = RESTHelper.replaceArgumentsWithWildcards(methodName);
val httpMethod = getHTTPMethod(m);
// Ignore HTTPMethod, only entire interfaces can be required right now.
// TODO: Find a way around this.
pcmDetector.detectCompositeRequiredInterface(identifier,
new RESTName(serviceIdentifier, methodName, httpMethod));
new RESTName(serviceIdentifier, methodName));
}
}
}
Expand Down Expand Up @@ -292,32 +294,32 @@ class SpringRules implements Rule {
def getHTTPMethod(MethodDeclaration m) {
val requestMapping = getMappingString(m, "RequestMapping");
if (requestMapping !== null) {
return HTTPMethod.any();
return HTTPMethod.WILDCARD;
}

val getMapping = getMappingString(m, "GetMapping");
if (getMapping !== null) {
return Set.of(HTTPMethod.GET);
return HTTPMethod.GET;
}

val postMapping = getMappingString(m, "PostMapping");
if (postMapping !== null) {
return Set.of(HTTPMethod.POST);
return HTTPMethod.POST;
}

val putMapping = getMappingString(m, "PutMapping");
if (putMapping !== null) {
return Set.of(HTTPMethod.PUT);
return HTTPMethod.PUT;
}

val deleteMapping = getMappingString(m, "DeleteMapping");
if (deleteMapping !== null) {
return Set.of(HTTPMethod.DELETE);
return HTTPMethod.DELETE;
}

val patchMapping = getMappingString(m, "PatchMapping");
if (patchMapping !== null) {
return Set.of(HTTPMethod.PATCH);
return HTTPMethod.PATCH;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.palladiosimulator.retriever.extraction.rules.data

import org.palladiosimulator.retriever.extraction.commonalities.RESTName
import org.palladiosimulator.retriever.extraction.commonalities.HTTPMethod

class GatewayRoute {
static final String TRAILING_WILDCARD = "/**"
Expand Down Expand Up @@ -42,7 +41,7 @@ class GatewayRoute {
val newUrlSegments = urlSegments.subList(stripPrefixLength, urlSegments.length)
newUrl = toPath(newUrlSegments)
}
return new RESTName(targetHost, newUrl, HTTPMethod.any)
return new RESTName(targetHost, newUrl)
}

private static def calculateStripPrefixLength(String pathPattern) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package org.palladiosimulator.retriever.extraction.commonalities;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public enum HTTPMethod {
GET, POST, PUT, DELETE, PATCH;
GET, POST, PUT, DELETE, PATCH,
/**
* Special value signaling all HTTPMethods are provided/required.
*/
WILDCARD;

public static boolean areAllPresent(Collection<HTTPMethod> httpMethods) {
return httpMethods.containsAll(all());
}

public static Set<HTTPMethod> all() {
return Set.of(values());
}
if (httpMethods.contains(HTTPMethod.WILDCARD)) {
return true;
}

public static Set<HTTPMethod> any() {
return Set.of();
Set<HTTPMethod> normalValues = new HashSet<>(Set.of(values()));
normalValues.remove(WILDCARD);
return httpMethods.containsAll(normalValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
import java.util.Set;
import java.util.Stack;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RESTName implements InterfaceName, OperationName {
public class RESTName implements InterfaceName {
private final String host;
private final List<String> path;
private final Set<HTTPMethod> httpMethods;

public RESTName(final String host, final String path, final Set<HTTPMethod> httpMethods)
throws IllegalArgumentException {
public RESTName(final String host, final String path) throws IllegalArgumentException {
this.host = host;
this.httpMethods = httpMethods;
final Optional<List<String>> parsedPath = parsePath(host + path);
if (parsedPath.isEmpty()) {
throw new IllegalArgumentException("Could not parse path due to illegal format: \"" + path + "\"");
Expand All @@ -29,30 +25,11 @@ public RESTName(final String host, final String path, final Set<HTTPMethod> http
this.path.remove(0);
}

public RESTName(final String host, final String path, final HTTPMethod... httpMethods)
throws IllegalArgumentException {
this(host, path, Set.of(httpMethods));
}

@Override
public String getName() {
return this.getInterface();
}

@Override
public String getInterface() {
return this.toString();
}

@Override
public Optional<String> forInterface(final String baseInterface) {
if (!this.isPartOf(baseInterface)) {
return Optional.empty();
}

return Optional.of(this.getInterface());
}

@Override
public List<String> getInterfaces() {
final Stack<List<String>> prefixes = new Stack<>();
Expand All @@ -68,10 +45,6 @@ public List<String> getInterfaces() {

final List<String> interfaces = new ArrayList<>(prefixes.size());

if (!this.httpMethods.isEmpty()) {
interfaces.add(this.getInterface());
}

// Insert the prefixes in reverse since the most specific element is at index 0 there.
while (!prefixes.empty()) {
interfaces.add(this.toName(prefixes.pop()));
Expand Down Expand Up @@ -102,23 +75,7 @@ private String toName(final List<String> path) {

@Override
public String toString() {
final String pathString = this.toName(this.path);

String methodString = "";
if (!this.httpMethods.isEmpty()) {
methodString = "[";
if (HTTPMethod.areAllPresent(this.httpMethods)) {
methodString += "*";
} else {
List<String> httpMethodNames = this.httpMethods.stream()
.map(HTTPMethod::toString)
.collect(Collectors.toList());
methodString += String.join(",", httpMethodNames);
}
methodString += "]";
}

return pathString + methodString;
return this.toName(this.path);
}

private static Optional<List<String>> parsePath(final String string) {
Expand All @@ -138,7 +95,7 @@ private static Optional<List<String>> parsePath(final String string) {

@Override
public int hashCode() {
return Objects.hash(this.host, this.path, this.httpMethods);
return Objects.hash(this.host, this.path);
}

@Override
Expand All @@ -150,8 +107,7 @@ public boolean equals(final Object obj) {
return false;
}
final RESTName other = (RESTName) obj;
return Objects.equals(this.host, other.host) && Objects.equals(this.path, other.path)
&& Objects.equals(this.httpMethods, other.httpMethods);
return Objects.equals(this.host, other.host) && Objects.equals(this.path, other.path);
}

@Override
Expand All @@ -170,10 +126,7 @@ public Optional<String> getCommonInterface(final Name other) {
return Optional.empty();
}
String commonPath = toName(this.path.subList(0, commonSegments));
Set<HTTPMethod> httpMethodUnion = new HashSet<>();
httpMethodUnion.addAll(this.httpMethods);
httpMethodUnion.addAll(otherREST.httpMethods);
return Optional.of(new RESTName(this.host, commonPath, httpMethodUnion).toString());
return Optional.of(new RESTName(this.host, commonPath).toString());
} else {
// Implementation from Name
final Set<String> interfaces = new HashSet<>(this.getInterfaces());
Expand Down Expand Up @@ -207,36 +160,17 @@ public boolean isPartOf(final String iface) {
return false;
}
}
if (restIface.path.size() == this.path.size() && !restIface.httpMethods.containsAll(this.httpMethods)) {
return false;
}
return true;
}

private static Optional<RESTName> parse(final String iface) {
final String[] parts = iface.split("\\[");
final Optional<List<String>> interfacePathOption = parsePath(parts[0]);
public static Optional<RESTName> parse(final String iface) {
final Optional<List<String>> interfacePathOption = parsePath(iface);
if (interfacePathOption.isEmpty()) {
return Optional.empty();
}
final List<String> path = interfacePathOption.get();
final String host = path.remove(0);

// If no HTTP method is specified, handle it like a wildcard.
Set<HTTPMethod> httpMethods = HTTPMethod.all();
if (parts.length > 1) {
// Assume that a '[' implies a ']'.
final int end = parts[1].lastIndexOf(']');
final String httpMethodNames = parts[1].substring(0, end);

if (!httpMethodNames.equals("*")) {
// Narrow the scope.
httpMethods = Stream.of(httpMethodNames.split(","))
.map(HTTPMethod::valueOf)
.collect(Collectors.toSet());
}
}

return Optional.of(new RESTName(host, "/" + String.join("/", path), httpMethods));
return Optional.of(new RESTName(host, "/" + String.join("/", path)));
}
}
Loading

0 comments on commit 2741e20

Please sign in to comment.