View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api;
17  
18  import java.util.Locale;
19  import java.util.stream.Collectors;
20  
21  import javax.annotation.Resource;
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.codelibs.fess.app.service.AccessTokenService;
25  import org.codelibs.fess.app.web.api.ApiResult.ApiErrorResponse;
26  import org.codelibs.fess.app.web.api.ApiResult.Status;
27  import org.codelibs.fess.app.web.base.FessBaseAction;
28  import org.codelibs.fess.mylasta.action.FessMessages;
29  import org.dbflute.optional.OptionalThing;
30  import org.lastaflute.core.message.MessageManager;
31  import org.lastaflute.web.login.LoginManager;
32  import org.lastaflute.web.response.ActionResponse;
33  import org.lastaflute.web.ruts.process.ActionRuntime;
34  import org.lastaflute.web.validation.VaMessenger;
35  
36  public abstract class FessApiAction extends FessBaseAction {
37  
38      @Resource
39      protected MessageManager messageManager;
40  
41      @Resource
42      protected AccessTokenService accessTokenService;
43  
44      @Resource
45      protected HttpServletRequest request;
46  
47      @Override
48      protected OptionalThing<LoginManager> myLoginManager() {
49          return OptionalThing.empty();
50      }
51  
52      @Override
53      public ActionResponse godHandPrologue(final ActionRuntime runtime) {
54          if (!isAccessAllowed()) {
55              return asJson(new ApiErrorResponse().message(getMessage(messages -> messages.addErrorsUnauthorizedRequest(GLOBAL)))
56                      .status(Status.UNAUTHORIZED).result());
57          }
58          return super.godHandPrologue(runtime);
59      }
60  
61      protected String getMessage(final VaMessenger<FessMessages> validationMessagesLambda) {
62          final FessMessages messages = new FessMessages();
63          validationMessagesLambda.message(messages);
64          return messageManager.toMessageList(request.getLocale() == null ? Locale.ENGLISH : request.getLocale(), messages).stream()
65                  .collect(Collectors.joining(" "));
66      }
67  
68      protected boolean isAccessAllowed() {
69          return false;
70      }
71  }