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.api;
17  
18  import java.io.IOException;
19  import java.io.OutputStreamWriter;
20  import java.io.PrintWriter;
21  import java.util.Locale;
22  
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.codelibs.core.exception.IORuntimeException;
26  import org.codelibs.fess.Constants;
27  import org.lastaflute.web.util.LaRequestUtil;
28  import org.lastaflute.web.util.LaResponseUtil;
29  
30  public abstract class BaseApiManager implements WebApiManager {
31  
32      protected String pathPrefix;
33  
34      protected enum FormatType {
35          SEARCH, LABEL, POPULARWORD, FAVORITE, FAVORITES, OTHER, PING;
36      }
37  
38      public String getPathPrefix() {
39          return pathPrefix;
40      }
41  
42      public void setPathPrefix(final String pathPrefix) {
43          this.pathPrefix = pathPrefix;
44      }
45  
46      protected FormatType getFormatType(final String formatType) {
47          if (formatType == null) {
48              return FormatType.SEARCH;
49          }
50          final String type = formatType.toUpperCase(Locale.ROOT);
51          if (FormatType.SEARCH.name().equals(type)) {
52              return FormatType.SEARCH;
53          } else if (FormatType.LABEL.name().equals(type)) {
54              return FormatType.LABEL;
55          } else if (FormatType.POPULARWORD.name().equals(type)) {
56              return FormatType.POPULARWORD;
57          } else if (FormatType.FAVORITE.name().equals(type)) {
58              return FormatType.FAVORITE;
59          } else if (FormatType.FAVORITES.name().equals(type)) {
60              return FormatType.FAVORITES;
61          } else if (FormatType.PING.name().equals(type)) {
62              return FormatType.PING;
63          } else {
64              // default
65              return FormatType.OTHER;
66          }
67      }
68  
69      public static void write(final String text, final String contentType, final String encoding) {
70          final StringBuilder buf = new StringBuilder(50);
71          if (contentType == null) {
72              buf.append("text/plain");
73          } else {
74              buf.append(contentType);
75          }
76          buf.append("; charset=");
77          final String enc;
78          if (encoding == null) {
79              if (LaRequestUtil.getRequest().getCharacterEncoding() == null) {
80                  enc = Constants.UTF_8;
81              } else {
82                  enc = LaRequestUtil.getRequest().getCharacterEncoding();
83              }
84          } else {
85              enc = encoding;
86          }
87          buf.append(enc);
88          final HttpServletResponse response = LaResponseUtil.getResponse();
89          response.setContentType(buf.toString());
90          try (PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), enc))) {
91              out.print(text);
92          } catch (final IOException e) {
93              throw new IORuntimeException(e);
94          }
95      }
96  
97  }