use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.
the class AdminEsreqAction method upload.
@Execute
public ActionResponse upload(final UploadForm form) {
validate(form, messages -> {
}, () -> asListHtml(null));
verifyToken(() -> asListHtml(() -> saveToken()));
String header = null;
final StringBuilder buf = new StringBuilder(1000);
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(form.requestFile.getInputStream(), Constants.UTF_8))) {
header = ReaderUtil.readLine(reader);
String line;
while ((line = ReaderUtil.readLine(reader)) != null) {
buf.append(line);
}
} catch (final Exception e) {
throwValidationError(messages -> messages.addErrorsFailedToReadRequestFile(GLOBAL, e.getMessage()), () -> {
return asListHtml(() -> saveToken());
});
}
final CurlRequest curlRequest = getCurlRequest(header);
if (curlRequest == null) {
final String msg = header;
throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, msg), () -> {
return asListHtml(() -> saveToken());
});
} else {
try (final CurlResponse response = curlRequest.header("Content-Type", "application/json").body(buf.toString()).execute()) {
final File tempFile = File.createTempFile("esreq_", ".json");
try (final InputStream in = response.getContentAsStream()) {
CopyUtil.copy(in, tempFile);
} catch (final Exception e1) {
if (tempFile != null && tempFile.exists() && !tempFile.delete()) {
logger.warn("Failed to delete " + tempFile.getAbsolutePath());
}
throw e1;
}
return asStream("es_" + System.currentTimeMillis() + ".json").contentTypeOctetStream().stream(out -> {
try (final InputStream in = new FileInputStream(tempFile)) {
out.write(in);
} finally {
if (tempFile.exists() && !tempFile.delete()) {
logger.warn("Failed to delete " + tempFile.getAbsolutePath());
}
}
});
} catch (final Exception e) {
logger.warn("Failed to process request file: " + form.requestFile.getFileName(), e);
throwValidationError(messages -> messages.addErrorsInvalidHeaderForRequestFile(GLOBAL, e.getMessage()), () -> {
return asListHtml(() -> saveToken());
});
}
}
// no-op
return redirect(getClass());
}
use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.
the class AdminDictKuromojiAction method download.
@Execute
public ActionResponse download(final DownloadForm form) {
validate(form, messages -> {
}, () -> downloadpage(form.dictId));
verifyTokenKeep(() -> downloadpage(form.dictId));
return kuromojiService.getKuromojiFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), () -> downloadpage(form.dictId));
return null;
});
}
use of org.lastaflute.web.response.ActionResponse in project fess by codelibs.
the class AdminDictMappingAction method download.
@Execute
public ActionResponse download(final DownloadForm form) {
validate(form, messages -> {
}, () -> downloadpage(form.dictId));
verifyTokenKeep(() -> downloadpage(form.dictId));
return charMappingService.getCharMappingFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> downloadpage(form.dictId));
return null;
});
}
Aggregations