use of org.codelibs.curl.CurlResponse in project fess by codelibs.
the class EsApiManager method processRequest.
protected void processRequest(final HttpServletRequest request, final HttpServletResponse response, final String path) {
if (StringUtil.isNotBlank(path)) {
final String lowerPath = path.toLowerCase(Locale.ROOT);
if (lowerPath.endsWith(".html")) {
response.setContentType("text/html;charset=utf-8");
} else if (lowerPath.endsWith(".txt")) {
response.setContentType("text/plain");
} else if (lowerPath.endsWith(".css")) {
response.setContentType("text/css");
}
}
if ("/_plugin".equals(path) || path.startsWith("/_plugin/")) {
processPluginRequest(request, response, path.replaceFirst("^/_plugin", StringUtil.EMPTY));
return;
}
final Method httpMethod = Method.valueOf(request.getMethod().toUpperCase(Locale.ROOT));
final CurlRequest curlRequest = ComponentUtil.getCurlHelper().request(httpMethod, path);
final String contentType = request.getHeader("Content-Type");
if (StringUtil.isNotEmpty(contentType)) {
curlRequest.header("Content-Type", contentType);
}
request.getParameterMap().entrySet().stream().forEach(entry -> {
if (entry.getValue().length > 1) {
curlRequest.param(entry.getKey(), String.join(",", entry.getValue()));
} else if (entry.getValue().length == 1) {
curlRequest.param(entry.getKey(), entry.getValue()[0]);
}
});
try (final CurlResponse curlResponse = curlRequest.onConnect((req, con) -> {
con.setDoOutput(true);
if (httpMethod != Method.GET && request.getContentLength() > 2) {
try (ServletInputStream in = request.getInputStream();
OutputStream out = con.getOutputStream()) {
CopyUtil.copy(in, out);
} catch (final IOException e) {
throw new WebApiException(HttpServletResponse.SC_BAD_REQUEST, e);
}
}
}).execute()) {
try (ServletOutputStream out = response.getOutputStream();
InputStream in = curlResponse.getContentAsStream()) {
response.setStatus(curlResponse.getHttpStatusCode());
writeHeaders(response);
CopyUtil.copy(in, out);
} catch (final ClientAbortException e) {
logger.debug("Client aborts this request.", e);
}
} catch (final Exception e) {
if (!(e.getCause() instanceof ClientAbortException)) {
throw new WebApiException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
logger.debug("Client aborts this request.", e);
}
}
use of org.codelibs.curl.CurlResponse in project fess by codelibs.
the class DictionaryManager method getDictionaryFiles.
public DictionaryFile<? extends DictionaryItem>[] getDictionaryFiles() {
try (CurlResponse response = ComponentUtil.getCurlHelper().get("/_configsync/file").param("fields", "path,@timestamp").param("size", ComponentUtil.getFessConfig().getPageDictionaryMaxFetchSize()).execute()) {
final Map<String, Object> contentMap = response.getContent(OpenSearchCurl.jsonParser());
@SuppressWarnings("unchecked") final List<Map<String, Object>> fileList = (List<Map<String, Object>>) contentMap.get("file");
return fileList.stream().map(fileMap -> {
try {
final String path = fileMap.get("path").toString();
final Date timestamp = new SimpleDateFormat(Constants.DATE_FORMAT_ISO_8601_EXTEND_UTC).parse(fileMap.get("@timestamp").toString());
for (final DictionaryCreator creator : creatorList) {
final DictionaryFile<? extends DictionaryItem> file = creator.create(path, timestamp);
if (file != null) {
return file;
}
}
} catch (final Exception e) {
logger.warn("Failed to load {}", fileMap, e);
}
return null;
}).filter(file -> file != null).toArray(n -> new DictionaryFile<?>[n]);
} catch (final IOException e) {
throw new DictionaryException("Failed to access dictionaries", e);
}
}
use of org.codelibs.curl.CurlResponse in project fess by codelibs.
the class NotificationHelper method sendToGoogleChat.
protected void sendToGoogleChat(final CardView cardView, final SMailPostingDiscloser discloser) {
// https://developers.google.com/hangouts/chat/how-tos/webhooks
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String googleChatWebhookUrls = fessConfig.getGoogleChatWebhookUrls();
if (StringUtil.isBlank(googleChatWebhookUrls)) {
return;
}
final String body = toGoogleChatMessage(discloser);
StreamUtil.split(googleChatWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
if (response.getHttpStatusCode() == 200) {
if (logger.isDebugEnabled()) {
logger.debug("Sent {} to {}.", body, url);
}
} else {
logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(), response.getContentAsString());
}
} catch (final IOException e) {
logger.warn("Failed to send {} to {}.", body, url, e);
}
}));
}
use of org.codelibs.curl.CurlResponse in project fess by codelibs.
the class NotificationHelper method sendToSlack.
protected void sendToSlack(final CardView cardView, final SMailPostingDiscloser discloser) {
// https://api.slack.com/messaging/webhooks#posting_with_webhooks
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final String slackWebhookUrls = fessConfig.getSlackWebhookUrls();
if (StringUtil.isBlank(slackWebhookUrls)) {
return;
}
final String body = toSlackMessage(discloser);
StreamUtil.split(slackWebhookUrls, "[,\\s]").of(stream -> stream.filter(StringUtil::isNotBlank).forEach(url -> {
try (CurlResponse response = Curl.post(url).header("Content-Type", "application/json").body(body).execute()) {
if (response.getHttpStatusCode() == 200) {
if (logger.isDebugEnabled()) {
logger.debug("Sent {} to {}.", body, url);
}
} else {
logger.warn("Failed to send {} to {}. HTTP Status is {}. {}", body, url, response.getHttpStatusCode(), response.getContentAsString());
}
} catch (final IOException e) {
logger.warn("Failed to send {} to {}.", body, url, e);
}
}));
}
use of org.codelibs.curl.CurlResponse in project fess by codelibs.
the class PluginHelper method install.
protected void install(final Artifact artifact) {
final String fileName = artifact.getFileName();
final String url = artifact.getUrl();
if (StringUtil.isBlank(url)) {
throw new PluginException("url is blank: " + artifact.getName());
}
if (url.startsWith("http:") || url.startsWith("https:")) {
try (final CurlResponse response = createCurlRequest(url).execute()) {
if (response.getHttpStatusCode() != 200) {
throw new PluginException("HTTP Status " + response.getHttpStatusCode() + " : failed to get the artifact from " + url);
}
try (final InputStream in = response.getContentAsStream()) {
CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
}
} catch (final Exception e) {
throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
}
} else {
try (final InputStream in = new FileInputStream(url)) {
CopyUtil.copy(in, ResourceUtil.getPluginPath(fileName).toFile());
} catch (final Exception e) {
throw new PluginException("Failed to install the artifact " + artifact.getName(), e);
}
}
}
Aggregations