View Javadoc
1   /*
2    * Copyright 2012-2017 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.codelibs.fess.mylasta.direction.FessConfig;
30  import org.dbflute.optional.OptionalThing;
31  import org.lastaflute.core.message.MessageManager;
32  import org.lastaflute.web.login.LoginManager;
33  import org.lastaflute.web.response.ActionResponse;
34  import org.lastaflute.web.ruts.process.ActionRuntime;
35  import org.lastaflute.web.validation.VaMessenger;
36  
37  public abstract class FessApiAction extends FessBaseAction {
38  
39      @Resource
40      protected FessConfig fessConfig;
41  
42      @Resource
43      protected MessageManager messageManager;
44  
45      @Resource
46      protected AccessTokenService accessTokenService;
47  
48      @Resource
49      protected HttpServletRequest request;
50  
51      @Override
52      protected OptionalThing<LoginManager> myLoginManager() {
53          return OptionalThing.empty();
54      }
55  
56      @Override
57      public ActionResponse godHandPrologue(final ActionRuntime runtime) {
58          if (!isAccessAllowed()) {
59              return asJson(new ApiErrorResponse().message(getMessage(messages -> messages.addErrorsUnauthorizedRequest(GLOBAL)))
60                      .status(Status.UNAUTHORIZED).result());
61          }
62          return super.godHandPrologue(runtime);
63      }
64  
65      protected String getMessage(final VaMessenger<FessMessages> validationMessagesLambda) {
66          final FessMessages messages = new FessMessages();
67          validationMessagesLambda.message(messages);
68          return messageManager.toMessageList(request.getLocale() == null ? Locale.ENGLISH : request.getLocale(), messages).stream()
69                  .collect(Collectors.joining(" "));
70      }
71  
72      protected boolean isAccessAllowed() {
73          return false;
74      }
75  }