diff --git a/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java b/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java index 2418c9b54..ff3205ec1 100644 --- a/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java +++ b/src/main/java/org/codelibs/fess/app/web/admin/log/AdminLogAction.java @@ -32,6 +32,8 @@ import java.util.stream.Stream; import org.codelibs.core.lang.StringUtil; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.exception.FessSystemException; +import org.codelibs.fess.helper.SystemHelper; +import org.codelibs.fess.util.ComponentUtil; import org.codelibs.fess.util.RenderDataUtil; import org.lastaflute.di.exception.IORuntimeException; import org.lastaflute.web.Execute; @@ -74,7 +76,8 @@ public class AdminLogAction extends FessAdminAction { return redirect(getClass()); // no-op } - private List> getLogFileItems() { + public static List> getLogFileItems() { + final SystemHelper systemHelper = ComponentUtil.getSystemHelper(); final List> logFileItems = new ArrayList<>(); final String logFilePath = systemHelper.getLogFilePath(); if (StringUtil.isNotBlank(logFilePath)) { diff --git a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java index 5b1871d33..fedc9970a 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java +++ b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java @@ -17,6 +17,7 @@ package org.codelibs.fess.app.web.api; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.stream.Collectors; import org.codelibs.fess.Constants; @@ -148,6 +149,26 @@ public class ApiResult { } } + public static class ApiLogFilesResponse extends ApiResponse { + protected List> logfiles; + protected long total = 0; + + public ApiLogFilesResponse logfiles(final List> logfiles) { + this.logfiles = logfiles; + return this; + } + + public ApiLogFilesResponse total(final long total) { + this.total = total; + return this; + } + + @Override + public ApiResult result() { + return new ApiResult(this); + } + } + public static class ApiErrorResponse extends ApiResponse { protected String message; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/log/ApiAdminLogAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/log/ApiAdminLogAction.java new file mode 100644 index 000000000..f019d4de1 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/log/ApiAdminLogAction.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2017 CodeLibs Project and the Others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.codelibs.fess.app.web.api.admin.log; + +import static org.codelibs.fess.app.web.admin.log.AdminLogAction.getLogFileItems; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.List; +import java.util.Map; + +import org.codelibs.core.lang.StringUtil; +import org.codelibs.fess.app.web.api.ApiResult; +import org.codelibs.fess.app.web.api.admin.FessApiAdminAction; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; +import org.lastaflute.web.response.StreamResponse; + +/** + * @author Keiichi Watanabe + */ +public class ApiAdminLogAction extends FessApiAdminAction { + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/log/logfiles + @Execute + public JsonResponse logfiles() { + List> list = getLogFileItems(); + return asJson(new ApiResult.ApiLogFilesResponse().logfiles(list).total(list.size()).status(ApiResult.Status.OK).result()); + } + + // GET /api/admin/log/logfile/{id} + @Execute + public StreamResponse get$logfile(final String id) { + final String filename = new String(Base64.getDecoder().decode(id), StandardCharsets.UTF_8).replace("..", "").replaceAll("\\s", ""); + final String logFilePath = systemHelper.getLogFilePath(); + if (StringUtil.isNotBlank(logFilePath)) { + final Path path = Paths.get(logFilePath, filename); + return asStream(filename).contentTypeOctetStream().stream(out -> { + try (InputStream in = Files.newInputStream(path)) { + out.write(in); + } + }); + } + return StreamResponse.asEmptyBody(); + } +}