Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/aol/micro-server into ena…
Browse files Browse the repository at this point in the history
…ble_s3_default_chain
  • Loading branch information
jijisv committed Oct 13, 2017
2 parents baefa86 + 332672b commit be10a52
Show file tree
Hide file tree
Showing 23 changed files with 666 additions and 101 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ task wrapper(type: Wrapper) {
}

subprojects {



apply plugin:'java'
// apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'com.bmuschko.nexus'
if(project.name != "micro-tutorial") {
apply plugin: 'com.bmuschko.nexus'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.91.5
version=0.91.8
springVersion=4.3.3.RELEASE
springBootVersion=1.4.1.RELEASE
jerseyVersion=2.24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
Expand All @@ -24,8 +26,13 @@ public Finder(RegisterConfig config) {
this.config = config;
}

public List<RegisterEntry> find() {
return findDir(new File(config.getOutputDir()));
public List<RegisterEntry> find(final Optional<RegisterEntry> re) {

List<RegisterEntry> entries = findDir(new File(config.getOutputDir()));
if (re.isPresent()) {
entries = entries.stream().filter( e -> e.matches(re.get())).collect(Collectors.toList());
}
return entries;
}

private List<RegisterEntry> findDir(File dir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.aol.micro.server.application.registry;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;

import javax.ws.rs.QueryParam;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
Expand All @@ -14,6 +12,7 @@
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.FieldDefaults;
import lombok.experimental.Wither;

Expand All @@ -24,43 +23,44 @@
@Getter
@Wither
@Builder
@ToString
public class RegisterEntry {

private static SimpleDateFormat f = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss Z");
private static SimpleDateFormat f = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
@Wither
int port;
private final int port;
@Wither
String hostname;
private final String hostname;
@Wither
String module;
private final String module;
@Wither
String context;
Date time;
private final String context;
private final Date time;
@Wither
String uuid;
private final String uuid;
@Wither
String target;
String formattedDate;
Map<String, String> manifest = ManifestLoader.instance.getManifest();
private final String target;
private final String formattedDate;
private final Map<String, String> manifest = new HashMap<>();
@Wither
Health health;
private final Health health;
@Wither
List<Map<String, Map<String, String>>> stats;
private final List<Map<String, Map<String, String>>> stats;
@Wither
int externalPort;
private final int externalPort;

public RegisterEntry() {
this(
-1, null, null, null, null, null, null, -1);
this(-1, null, null, null, null, null, null, -1);
}

public RegisterEntry(int port, String hostname, String module, String context, Date time, String uuid,
String target, int externalPort) {
this(
port, hostname, module, context, time, UUID.randomUUID()
.toString(),
target, null, Health.OK, null, externalPort);
String target, int externalPort) {
this(port, hostname, module, context, time, uuid, target, null, Health.OK, null, externalPort);
}

public RegisterEntry(int port, String hostname, String module, String context, Date time, String target,
int externalPort) {
this(port, hostname, module, context, time, UUID.randomUUID().toString(), target, externalPort);
}

private RegisterEntry(int port, String hostname, String module, String context, Date time, String uuid,
Expand All @@ -82,14 +82,28 @@ private RegisterEntry(int port, String hostname, String module, String context,
else
this.formattedDate = null;

this.manifest.putAll(ManifestLoader.instance.getManifest());

}

public RegisterEntry(int port, String hostname, String module, String context, Date time, String target,
int externalPort) {
this(
port, hostname, module, context, time, UUID.randomUUID()
.toString(),
target, externalPort);
public boolean matches(RegisterEntry re) {
//Only the fields which make sense to query is added for now.
return (re.port == -1 || re.port == port) &&
(Objects.isNull(re.hostname) || Objects.nonNull(hostname) && hostname.startsWith(re.hostname)) &&
(Objects.isNull(re.module) || Objects.nonNull(module) && module.startsWith(re.module)) &&
(Objects.isNull(re.context) || Objects.nonNull(context) && context.startsWith(re.context)) &&
(Objects.isNull(re.health) || re.health.equals(health)) &&
(re.externalPort == -1 || re.externalPort == externalPort) &&
(Objects.isNull(re.manifest) || re.manifest.isEmpty() || matchManifest(re.manifest));
}

private boolean matchManifest(Map<String, String> manifest) {
return match(manifest, this.manifest, "Implementation-revision") &&
match(manifest, this.manifest, "Implementation-Timestamp") &&
match(manifest, this.manifest, "Implementation-Version");
}

}
private boolean match(Map<String, String> map1, Map<String, String> map2, String key) {
return !map1.containsKey(key) || (map2.containsKey(key) && map2.get(key).startsWith(map1.get(key)));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.aol.micro.server.application.registry;

import java.util.Arrays;
import java.util.Optional;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import cyclops.stream.ReactiveSeq;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,8 @@
import com.aol.micro.server.auto.discovery.Rest;
import com.aol.micro.server.utility.HashMapBuilder;

import static javax.ws.rs.core.Response.Status.*;


@Rest
@Path("/service-registry")
Expand All @@ -42,18 +45,17 @@ public ServiceRegistryResource(Cleaner cleaner, Finder finder, Register register
@GET
@Path("/list")
@Produces("application/json")
public void list(@Suspended AsyncResponse response) {
public void list(@Context UriInfo uriInfo, @Suspended AsyncResponse response) {
ReactiveSeq.of(this).foldFuture(WorkerThreads.ioExecutor.get(),
s->s.forEach(Long.MAX_VALUE,next -> {
try{
cleaner.clean();
response.resume(finder.find());
response.resume(finder.find(UriInfoParser.toRegisterEntry(uriInfo)));
}catch(Exception e){
logger.error(e.getMessage(),e);
response.resume(Arrays.asList("failed due to error"));
response.resume(Arrays.asList("Bad Request: " + e.getMessage()));
}
}));

}

@POST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.aol.micro.server.application.registry;

import cyclops.stream.ReactiveSeq;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class UriInfoParser {

public static Optional<RegisterEntry> toRegisterEntry(UriInfo uriInfo) {
if (uriInfo.getQueryParameters().isEmpty()) {
return Optional.empty();
} else {
MultivaluedMap<String, String> parameters = uriInfo.getQueryParameters();
RegisterEntry re = RegisterEntry.builder()
.context(parameters.getFirst("context"))
.hostname(parameters.getFirst("hostname"))
.port(toInt(parameters.getFirst("port")))
.target(parameters.getFirst("target"))
.externalPort(toInt(parameters.getFirst("externalPort")))
.module(parameters.getFirst("module"))
.health(toHealth(parameters.getFirst("health")))
.build();

Map<String, String> manifest = ReactiveSeq.fromIterable(parameters.entrySet())
.filter(e -> e.getKey().startsWith("manifest."))
.toMap(e -> e.getKey().replace("manifest.", ""), e -> parameters.getFirst(e.getKey()));

re.getManifest().clear();
re.getManifest().putAll(manifest);

return Optional.of(re);
}
}

private static Health toHealth(String health) {
if (Objects.nonNull(health)) {
try {
return Health.valueOf(health);
} catch (Exception e) {
throw new IllegalArgumentException("'" + health + "' is not valid, valid values are " +
Arrays.asList(Health.values()));
}
}
return null;
}

private static int toInt(String port) {
if (Objects.isNull(port))
return -1;

try {
return Integer.valueOf(port);
} catch (Exception e) {
throw new IllegalArgumentException("'" + port + "' is not a valid number.");
}
}

}
Loading

0 comments on commit be10a52

Please sign in to comment.