1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.codelibs.core.exception.IORuntimeException;
27 import org.codelibs.fess.Constants;
28 import org.lastaflute.web.util.LaRequestUtil;
29 import org.lastaflute.web.util.LaResponseUtil;
30
31 public abstract class BaseApiManager implements WebApiManager {
32
33 private static final String API_FORMAT_TYPE = "apiFormatType";
34
35 protected String pathPrefix;
36
37 protected enum FormatType {
38 SEARCH, LABEL, POPULARWORD, FAVORITE, FAVORITES, OTHER, PING, SCROLL;
39 }
40
41 public String getPathPrefix() {
42 return pathPrefix;
43 }
44
45 public void setPathPrefix(final String pathPrefix) {
46 this.pathPrefix = pathPrefix;
47 }
48
49 protected FormatType getFormatType(final HttpServletRequest request) {
50 FormatType formatType = (FormatType) request.getAttribute(API_FORMAT_TYPE);
51 if (formatType != null) {
52 return formatType;
53 }
54 String value = request.getParameter("type");
55 if (value == null) {
56 final String servletPath = request.getServletPath();
57 final String[] values = servletPath.replaceAll("/+", "/").split("/");
58 if (values.length > 2) {
59 value = values[2];
60 }
61 }
62 if (value == null) {
63 formatType = FormatType.SEARCH;
64 } else {
65 final String type = value.toUpperCase(Locale.ROOT);
66 if (FormatType.SEARCH.name().equals(type)) {
67 formatType = FormatType.SEARCH;
68 } else if (FormatType.LABEL.name().equals(type)) {
69 formatType = FormatType.LABEL;
70 } else if (FormatType.POPULARWORD.name().equals(type)) {
71 formatType = FormatType.POPULARWORD;
72 } else if (FormatType.FAVORITE.name().equals(type)) {
73 formatType = FormatType.FAVORITE;
74 } else if (FormatType.FAVORITES.name().equals(type)) {
75 formatType = FormatType.FAVORITES;
76 } else if (FormatType.PING.name().equals(type)) {
77 formatType = FormatType.PING;
78 } else if (FormatType.SCROLL.name().equals(type)) {
79 formatType = FormatType.SCROLL;
80 } else {
81
82 formatType = FormatType.OTHER;
83 }
84 }
85 request.setAttribute(API_FORMAT_TYPE, formatType);
86 return formatType;
87 }
88
89 protected void write(final String text, final String contentType, final String encoding) {
90 final StringBuilder buf = new StringBuilder(50);
91 if (contentType == null) {
92 buf.append("text/plain");
93 } else {
94 buf.append(contentType);
95 }
96 buf.append("; charset=");
97 final String enc;
98 if (encoding == null) {
99 if (LaRequestUtil.getRequest().getCharacterEncoding() == null) {
100 enc = Constants.UTF_8;
101 } else {
102 enc = LaRequestUtil.getRequest().getCharacterEncoding();
103 }
104 } else {
105 enc = encoding;
106 }
107 buf.append(enc);
108 final HttpServletResponse response = LaResponseUtil.getResponse();
109 response.setContentType(buf.toString());
110 writeHeaders(response);
111 try (PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), enc))) {
112 out.print(text);
113 } catch (final IOException e) {
114 throw new IORuntimeException(e);
115 }
116 }
117
118 protected abstract void writeHeaders(final HttpServletResponse response);
119 }