use of java.nio.charset.StandardCharsets.UTF_8 in project gerrit by GerritCodeReview.
the class RawMailParser method parse.
/**
* Parses a MailMessage from a string.
*
* @param raw String as received over the wire
* @return Parsed MailMessage
* @throws MailParsingException
*/
public static MailMessage parse(String raw) throws MailParsingException {
MailMessage.Builder messageBuilder = MailMessage.builder();
messageBuilder.rawContentUTF(raw);
Message mimeMessage;
try {
MessageBuilder builder = new DefaultMessageBuilder();
mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
} catch (IOException | MimeException e) {
throw new MailParsingException("Can't parse email", e);
}
// Add general headers
if (mimeMessage.getMessageId() != null) {
messageBuilder.id(mimeMessage.getMessageId());
}
if (mimeMessage.getSubject() != null) {
messageBuilder.subject(mimeMessage.getSubject());
}
messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
// Add From, To and Cc
if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
Mailbox from = mimeMessage.getFrom().get(0);
messageBuilder.from(new Address(from.getName(), from.getAddress()));
}
if (mimeMessage.getTo() != null) {
for (Mailbox m : mimeMessage.getTo().flatten()) {
messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
}
}
if (mimeMessage.getCc() != null) {
for (Mailbox m : mimeMessage.getCc().flatten()) {
messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
}
}
// Add additional headers
mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
// Add text and html body parts
StringBuilder textBuilder = new StringBuilder();
StringBuilder htmlBuilder = new StringBuilder();
try {
handleMimePart(mimeMessage, textBuilder, htmlBuilder);
} catch (IOException e) {
throw new MailParsingException("Can't parse email", e);
}
messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
try {
// required attributes are missing, so that the caller doesn't fall over.
return messageBuilder.build();
} catch (IllegalStateException e) {
throw new MailParsingException("Missing required attributes after email was parsed", e);
}
}
use of java.nio.charset.StandardCharsets.UTF_8 in project protoman by spotify.
the class GcsSchemaStorage method open.
@Override
public Transaction open() {
final ProtoIndex protoIndex = ProtoIndex.parse(indexFile.load());
long indexGeneration = indexFile.currentGeneration();
logger.debug("Starting transaction from snapshot={}", indexGeneration);
return new Transaction() {
final AtomicReference<TxState> state = new AtomicReference<>(TxState.OPEN);
@Override
public void storeFile(final SchemaFile file) {
Preconditions.checkState(state.get() == TxState.OPEN);
final HashCode hash = protoStorage.put(file.content().getBytes(Charsets.UTF_8));
protoIndex.updateProtoLocation(file.path().toString(), hash.toString());
logger.info("Stored file. path={} content={}", file.path(), hash.toString());
}
@Override
public Stream<SchemaFile> fetchAllFiles(final long snapshotVersion) {
Preconditions.checkState(state.get() == TxState.OPEN);
ProtoIndex currentProtoIndex = protoIndex(snapshotVersion);
return currentProtoIndex.getProtoLocations().entrySet().stream().map(e -> schemaFile(Paths.get(e.getKey()), e.getValue()));
}
@Override
public void storePackageVersion(final String protoPackage, final SchemaVersion version) {
Preconditions.checkState(state.get() == TxState.OPEN);
protoIndex.updatePackageVersion(protoPackage, version);
}
@Override
public void storeProtoDependencies(final Path path, final Set<Path> paths) {
Preconditions.checkState(state.get() == TxState.OPEN);
protoIndex.updateProtoDependencies(path, paths);
}
@Override
public Optional<SchemaVersion> getPackageVersion(final long snapshotVersion, final String protoPackage) {
Preconditions.checkState(state.get() == TxState.OPEN);
final SchemaVersion schemaVersion = protoIndex(snapshotVersion).getPackageVersions().get(protoPackage);
return Optional.ofNullable(schemaVersion);
}
@Override
public Stream<Path> protosForPackage(final long snapshotVersion, final String pkgName) {
Preconditions.checkState(state.get() == TxState.OPEN);
ProtoIndex currentProtoIndex = protoIndex(snapshotVersion);
return currentProtoIndex.getProtoLocations().keySet().stream().map(Paths::get).filter(packageFilter(pkgName));
}
@Override
public Stream<Path> getDependencies(final long snapshotVersion, final Path path) {
Preconditions.checkState(state.get() == TxState.OPEN);
ProtoIndex currentProtoIndex = protoIndex(snapshotVersion);
return currentProtoIndex.getProtoDependencies().get(path).stream();
}
@Override
public SchemaFile schemaFile(final long snapshotVersion, final Path path) {
Preconditions.checkState(state.get() == TxState.OPEN);
ProtoIndex currentProtoIndex = protoIndex(snapshotVersion);
return SchemaFile.create(path, fileContents(currentProtoIndex, path));
}
@Override
public ImmutableMap<String, SchemaVersion> allPackageVersions(final long snapshotVersion) {
Preconditions.checkState(state.get() == TxState.OPEN);
return ImmutableMap.copyOf(protoIndex(snapshotVersion).getPackageVersions());
}
@Override
public long commit() {
Preconditions.checkState(state.compareAndSet(TxState.OPEN, TxState.COMMITTED));
final long snapshotVersion = indexFile.replace(protoIndex.toByteArray());
logger.info("Committed. snapshotVersion={}", snapshotVersion);
if (logger.isDebugEnabled()) {
logger.debug("index={}", protoIndex.toProtoString());
}
return snapshotVersion;
}
@Override
public long getLatestSnapshotVersion() {
Preconditions.checkState(state.get() == TxState.OPEN);
return indexFile.currentGeneration();
}
@Override
public Stream<Long> getSnapshotVersions() {
Preconditions.checkState(state.get() == TxState.OPEN);
return indexFile.listGenerations();
}
@Override
public void deleteFile(final Path path) {
Preconditions.checkState(state.get() == TxState.OPEN);
if (!protoIndex.removeProtoLocation(path.toString())) {
throw new RuntimeException("Not found: " + path);
}
}
@Override
public void close() {
Preconditions.checkState(state.getAndSet(TxState.CLOSED) != TxState.CLOSED);
// nothing do to
}
private Predicate<Path> packageFilter(final String pkgName) {
Objects.requireNonNull(pkgName);
final Path pkgPath = Paths.get(pkgName.replaceAll("\\.", "/"));
return path -> path.getParent().equals(pkgPath);
}
private String fileContents(final ProtoIndex protoIndex, final Path path) {
Objects.requireNonNull(protoIndex);
Objects.requireNonNull(path);
final String location = protoIndex.getProtoLocations().get(path.toString());
if (location == null) {
throw new RuntimeException("Location not found: " + path);
}
final byte[] bytes = protoStorage.get(HashCode.fromString(location)).orElseThrow(() -> new IllegalStateException("Location found. Missing data: " + path));
return new String(bytes, UTF_8);
}
private ProtoIndex protoIndex(final long snapshotVersion) {
if (snapshotVersion != indexGeneration) {
return ProtoIndex.parse(indexFile.contentForGeneration(snapshotVersion));
}
return protoIndex;
}
private SchemaFile schemaFile(final Path path, final String hash) {
Objects.requireNonNull(path);
Objects.requireNonNull(hash);
return SchemaFile.create(path, new String(protoStorage.get(HashCode.fromString(hash)).orElseThrow(() -> new RuntimeException("Not found: " + hash)), Charsets.UTF_8));
}
};
}
use of java.nio.charset.StandardCharsets.UTF_8 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 java.nio.charset.StandardCharsets.UTF_8 in project data-prep by Talend.
the class TransformationService method getSemanticDomains.
/**
* Return the semantic domains for the given parameters.
*
* @param metadata the dataset metadata.
* @param columnId the column id to analyze.
* @param records the dataset records.
* @return the semantic domains for the given parameters.
* @throws IOException can happen...
*/
private List<SemanticDomain> getSemanticDomains(DataSetMetadata metadata, String columnId, InputStream records) throws IOException {
// copy the column metadata and set the semantic domain forced flag to false to make sure the statistics adapter set all
// available domains
final ColumnMetadata columnMetadata = //
column().copy(//
metadata.getRowMetadata().getById(columnId)).semanticDomainForce(//
false).build();
final Analyzer<Analyzers.Result> analyzer = analyzerService.build(columnMetadata, SEMANTIC);
analyzer.init();
try (final JsonParser parser = mapper.getFactory().createParser(new InputStreamReader(records, UTF_8))) {
final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
dataSet.getRecords().map(//
r -> r.get(columnId)).forEach(analyzer::analyze);
analyzer.end();
}
final List<Analyzers.Result> analyzerResult = analyzer.getResult();
statisticsAdapter.adapt(singletonList(columnMetadata), analyzerResult);
return columnMetadata.getSemanticDomains();
}
use of java.nio.charset.StandardCharsets.UTF_8 in project data-prep by Talend.
the class DataSetExportStrategy method execute.
@Override
public StreamingResponseBody execute(ExportParameters parameters) {
final String formatName = parameters.getExportType();
final ExportFormat format = getFormat(formatName);
//
ExportUtils.setExportHeaders(//
parameters.getExportName(), //
parameters.getArguments().get(ExportFormat.PREFIX + CSVFormat.ParametersCSV.ENCODING), format);
return outputStream -> {
// get the dataset content (in an auto-closable block to make sure it is properly closed)
final String datasetId = parameters.getDatasetId();
final DataSetGet dataSetGet = applicationContext.getBean(DataSetGet.class, datasetId, false, true);
final DataSetGetMetadata dataSetGetMetadata = applicationContext.getBean(DataSetGetMetadata.class, datasetId);
try (InputStream datasetContent = dataSetGet.execute()) {
try (JsonParser parser = mapper.getFactory().createParser(new InputStreamReader(datasetContent, UTF_8))) {
// Create dataset
final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
dataSet.setMetadata(dataSetGetMetadata.execute());
// get the actions to apply (no preparation ==> dataset export ==> no actions)
Configuration configuration = //
Configuration.builder().args(//
parameters.getArguments()).outFilter(//
rm -> filterService.build(parameters.getFilter(), rm)).format(//
format.getName()).volume(//
Configuration.Volume.SMALL).output(//
outputStream).limit(//
limit).build();
factory.get(configuration).buildExecutable(dataSet, configuration).execute();
}
} catch (TDPException e) {
throw e;
} catch (Exception e) {
throw new TDPException(TransformationErrorCodes.UNABLE_TO_TRANSFORM_DATASET, e);
}
};
}
Aggregations