use of oap.util.Stream in project oap by oaplatform.
the class Client method uploadFile.
@SneakyThrows
public Response uploadFile(String uri, String prefix, Path path) {
final ContentType contentType = ContentType.create(java.nio.file.Files.probeContentType(path));
OkHttpClient client = new OkHttpClient();
final MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("upfile", path.toFile().getName(), RequestBody.create(MediaType.parse(contentType.toString()), path.toFile())).addFormDataPart("prefix", prefix).build();
okhttp3.Request request = new okhttp3.Request.Builder().url(uri).post(body).build();
final okhttp3.Response response = client.newCall(request).execute();
final Headers headers = response.headers();
final java.util.stream.Stream<String> stream = headers.names().stream();
final Map<String, String> h = stream.collect(Collectors.toMap(n -> n, headers::get));
return new Response(response.code(), response.message(), h, Optional.ofNullable(response.body().contentType()).map(mt -> ContentType.create(mt.type() + "/" + mt.subtype(), mt.charset())), response.body().bytes());
}
use of oap.util.Stream in project oap by oaplatform.
the class WsService method handleInternal.
private void handleInternal(Request request, Response response, Reflection.Method method, Name name, Pair<String, Session> session) {
log.trace("{}: Internal session status: [{}]", service(), session);
Optional<WsMethod> wsMethod = method.findAnnotation(WsMethod.class);
Function<Reflection.Parameter, Object> func = (p) -> {
val ret = getValue(session, request, wsMethod, p).orElse(Optional.empty());
if (ret instanceof Optional)
return ((Optional<?>) ret).orElse(null);
return ret;
};
HttpResponse interceptorResponse = session != null ? runInterceptors(request, session._2, method, func) : null;
if (interceptorResponse != null) {
response.respond(interceptorResponse);
} else {
Metrics.measureTimer(name, () -> {
List<Reflection.Parameter> parameters = method.parameters;
LinkedHashMap<Reflection.Parameter, Object> originalValues = getOriginalValues(session, parameters, request, wsMethod);
ValidationErrors paramValidation = ValidationErrors.empty();
originalValues.forEach((parameter, value) -> paramValidation.merge(Validators.forParameter(method, parameter, impl, true).validate(value, originalValues)));
paramValidation.throwIfInvalid();
Validators.forMethod(method, impl, true).validate(originalValues.values().toArray(new Object[originalValues.size()]), originalValues).throwIfInvalid();
LinkedHashMap<Reflection.Parameter, Object> values = getValues(originalValues);
Object[] paramValues = values.values().toArray(new Object[values.size()]);
values.forEach((parameter, value) -> paramValidation.merge(Validators.forParameter(method, parameter, impl, false).validate(value, values)));
paramValidation.throwIfInvalid();
Validators.forMethod(method, impl, false).validate(paramValues, values).throwIfInvalid();
Object result = method.invoke(impl, paramValues);
Boolean isRaw = wsMethod.map(WsMethod::raw).orElse(false);
ContentType produces = wsMethod.map(wsm -> ContentType.create(wsm.produces()).withCharset(UTF_8)).orElse(APPLICATION_JSON);
String cookie = session != null ? new HttpResponse.CookieBuilder().withSID(session._1).withPath(sessionManager.cookiePath).withExpires(DateTime.now().plusMinutes(sessionManager.cookieExpiration)).withDomain(sessionManager.cookieDomain).withDomain(sessionManager.cookieDomain).build() : null;
if (method.isVoid())
response.respond(NO_CONTENT);
else if (result instanceof HttpResponse)
response.respond(((HttpResponse) result).withCookie(cookie));
else if (result instanceof Optional<?>) {
response.respond(((Optional<?>) result).map(r -> HttpResponse.ok(runPostInterceptors(r, session, method), isRaw, produces).withCookie(cookie)).orElse(NOT_FOUND));
} else if (result instanceof Result<?, ?>) {
Result<HttpResponse, HttpResponse> resp = ((Result<?, ?>) result).mapSuccess(r -> HttpResponse.ok(r, isRaw, produces).withCookie(cookie)).mapFailure(r -> HttpResponse.status(HTTP_INTERNAL_ERROR, "", r).withCookie(cookie));
response.respond(resp.isSuccess() ? ((Result<?, ?>) result).mapSuccess(r -> HttpResponse.ok(runPostInterceptors(r, session, method), isRaw, produces).withCookie(cookie)).successValue : ((Result<?, ?>) result).mapFailure(r -> HttpResponse.status(HTTP_INTERNAL_ERROR, "", r).withCookie(cookie)).failureValue);
} else if (result instanceof Stream<?>) {
response.respond(HttpResponse.stream(((Stream<?>) result).map(v -> runPostInterceptors(v, session, method)), isRaw, produces).withCookie(cookie));
} else
response.respond(HttpResponse.ok(runPostInterceptors(result, session, method), isRaw, produces).withCookie(cookie));
});
}
}
use of oap.util.Stream in project oap by oaplatform.
the class Tree method trace.
public String trace(List<?> query, Predicate<T> filter) {
final HashMap<T, HashMap<Integer, TraceOperationTypeValues>> result = new HashMap<>();
final long[][] longQuery = convertQueryToLong(query);
trace(root, longQuery, result, new TraceBuffer(), true);
final String queryStr = "query = " + Stream.of(query).zipWithIndex().map(p -> dimensions.get(p._2).name + ":" + printValue(p._1)).collect(joining(",", "[", "]")) + "\n";
final String out = result.entrySet().stream().filter(e -> filter.test(e.getKey())).map(e -> e.getKey().toString() + ": \n" + e.getValue().entrySet().stream().map(dv -> {
final Dimension dimension = dimensions.get(dv.getKey());
return " " + dimension.name + "/" + dv.getKey() + ": " + dv.getValue().toString(dimension) + " " + queryToString(query, dv.getKey());
}).collect(joining("\n"))).collect(joining("\n"));
return queryStr + (out.length() > 0 ? "Expecting:\n" + out : "ALL OK");
}
use of oap.util.Stream in project oap by oaplatform.
the class MongoStorage method fsync.
@Override
public void fsync() {
super.fsync();
val count = new MutableInt();
Stream.of(data.values().stream().filter(m -> m.modified >= lastFsync)).grouped(bulkSize).forEach(list -> {
count.add(list.size());
final List<? extends WriteModel<Metadata<T>>> bulk = Lists.map(list, metadata -> {
val id = getIdentifier().get(metadata.object);
return new ReplaceOneModel<>(eq("_id", new ObjectId(id)), metadata, UPDATE_OPTIONS_UPSERT);
});
collection.bulkWrite(bulk);
});
log.info("[{}] fsync total: {}, modified: {}", collection.getNamespace(), size(), count.intValue());
lastFsync = System.currentTimeMillis();
}
use of oap.util.Stream in project oap by oaplatform.
the class DictionaryMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
final List<Path> paths = Files.fastWildcard(Paths.get(sourceDirectory), "*.json").stream().filter(p -> {
final boolean b = !Arrays.stream(exclude).filter(e -> FilenameUtils.wildcardMatchOnSystem(separatorsToUnix(p.toString()), e)).findAny().isPresent();
if (!b)
getLog().debug("exclude " + p);
return b;
}).collect(toList());
getLog().debug("found " + paths);
paths.forEach(path -> {
getLog().info("dictionary " + path + "...");
final DictionaryRoot dictionary = DictionaryParser.parse(path);
final StringBuilder out = new StringBuilder();
final String enumClass = toEnumName(dictionary.name);
out.append("package " + dictionaryPackage + ";\n\n").append("import oap.dictionary.Dictionary;\n\n").append("import java.util.Map;\n").append("import java.util.Optional;\n").append("import java.util.List;\n\n").append("import static java.util.Collections.emptyList;\n").append("import static java.util.Collections.emptyMap;\n").append("import static java.util.Arrays.asList;\n\n").append("public enum " + enumClass + " implements Dictionary {\n");
final Set<String> properties = dictionary.getValues().stream().flatMap(v -> v.getProperties().keySet().stream()).collect(toSet());
final Map<String, Boolean> optional = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().anyMatch(v -> !v.containsProperty(k))));
final Map<String, Class<?>> types = properties.stream().collect(toMap(k -> k, k -> dictionary.getValues().stream().filter(v -> v.containsProperty(k)).findAny().get().getProperty(k).get().getClass()));
out.append(dictionary.getValues().stream().map(d -> " " + d.getId() + "(" + convert(d.getExternalId(), dictionary.externalIdAs) + ", " + d.isEnabled() + properties(d.getProperties(), properties, optional, types) + ")").collect(joining(",\n")));
final String externalIdType = dictionary.externalIdAs.javaType.getSimpleName();
out.append(";\n\n").append(" private final " + externalIdType + " externalId;\n").append(" private final boolean enabled;\n\n");
for (String property : properties) {
out.append(" private final " + propertyType(property, optional, types) + " " + property + ";\n");
}
out.append("\n");
for (String property : properties) {
out.append(" public final " + propertyType(property, optional, types) + " " + property + "(){return " + property + ";}\n");
}
out.append("\n " + enumClass + "( " + externalIdType + " externalId, boolean enabled");
final String cParameters = properties.stream().map(p -> propertyType(p, optional, types) + " " + p).collect(joining(", "));
out.append(cParameters.length() > 0 ? ", " : "").append(cParameters + " ) {\n").append(" this.externalId = externalId;\n").append(" this.enabled = enabled;\n");
for (String property : properties) {
out.append(" this." + property + " = " + property + ";\n");
}
out.append(" }\n" + "\n" + " public static " + enumClass + " valueOf( int externalId ) {\n" + " switch( externalId ) {\n");
dictionary.getValues().forEach(d -> {
out.append(" case ").append(d.getExternalId()).append(": return ").append(d.getId()).append(";\n");
});
out.append(" default: ");
if (dictionary.containsValueWithId("UNKNOWN")) {
out.append("return UNKNOWN");
} else {
out.append("throw new java.lang.IllegalArgumentException( \"Unknown id \" + externalId )");
}
out.append(";\n" + " }\n" + " }\n" + "\n" + " @Override\n" + " public int getOrDefault( String id, int defaultValue ) {\n" + " return defaultValue;\n" + " }\n" + "\n" + " @Override\n" + " public Integer get( String id ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public String getOrDefault( int externlId, String defaultValue ) {\n" + " return defaultValue;\n" + " }\n" + "\n" + " @Override\n" + " public boolean containsValueWithId( String id ) {\n" + " return false;\n" + " }\n" + "\n" + " @Override\n" + " public List<String> ids() {\n" + " return emptyList();\n" + " }\n" + "\n" + " @Override\n" + " public int[] externalIds() {\n" + " return new int[0];\n" + " }\n" + "\n" + " @Override\n" + " public Map<String, Object> getProperties() {\n" + " return emptyMap();\n" + " }\n" + "\n" + " @Override\n" + " public Optional<? extends Dictionary> getValueOpt( String name ) {\n" + " return Optional.empty();\n" + " }\n" + "\n" + " @Override\n" + " public Dictionary getValue( String name ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public Dictionary getValue( int externalId ) {\n" + " return null;\n" + " }\n" + "\n" + " @Override\n" + " public List<? extends Dictionary> getValues() {\n" + " return emptyList();\n" + " }\n" + "\n" + " @Override\n" + " public String getId() {\n" + " return name();\n" + " }\n" + "\n" + " @Override\n" + " public Optional<Object> getProperty( String name ) {\n" + " return Optional.empty();\n" + " }\n" + "\n" + " @Override\n" + " public boolean isEnabled() {\n" + " return enabled;\n" + " }\n" + "\n" + " @Override\n" + " public int getExternalId() {\n" + " return externalId;\n" + " }\n" + " public " + externalIdType + " externalId() {\n" + " return externalId;\n" + " }\n" + "\n" + " @Override\n" + " public boolean containsProperty( String name ) {\n" + " return false;\n" + " }\n").append("}\n");
final Path outPath = Paths.get(outputDirectory, dictionaryPackage.replace(".", "/"), enumClass + ".java");
if (!java.nio.file.Files.exists(outPath) || !Files.readString(outPath).equals(out.toString())) {
Files.writeString(outPath, IoStreams.Encoding.PLAIN, out.toString());
} else {
getLog().debug(outPath + " is not modified.");
}
});
}
Aggregations