1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.helper;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.codelibs.core.lang.StringUtil;
25 import org.codelibs.fess.Constants;
26 import org.codelibs.fess.mylasta.direction.FessConfig;
27 import org.codelibs.fess.util.ComponentUtil;
28 import org.lastaflute.web.response.StreamResponse;
29 import org.lastaflute.web.util.LaServletContextUtil;
30
31 import jakarta.annotation.PostConstruct;
32
33
34
35
36 public class OsddHelper {
37
38
39
40
41 public OsddHelper() {
42
43 }
44
45 private static final Logger logger = LogManager.getLogger(OsddHelper.class);
46
47
48 protected String osddPath;
49
50
51 protected String encoding = Constants.UTF_8;
52
53
54 protected String contentType = "text/xml";
55
56
57 protected File osddFile;
58
59
60
61
62 @PostConstruct
63 public void init() {
64 if (logger.isDebugEnabled()) {
65 logger.debug("Initializing {}", this.getClass().getSimpleName());
66 }
67 osddFile = getOsddFile();
68 }
69
70
71
72
73
74
75 protected File getOsddFile() {
76 if (!isOsddLinkEnabled()) {
77 logger.debug("OSDD is disabled.");
78 return null;
79 }
80 if (StringUtil.isBlank(osddPath)) {
81 logger.info("OSDD file is not found.");
82 return null;
83 }
84 final String path = LaServletContextUtil.getServletContext().getRealPath(osddPath);
85 if (path == null) {
86 logger.warn("OSDD file path could not be resolved: {}", osddPath);
87 return null;
88 }
89 final File osddFile = new File(path);
90 if (!osddFile.isFile()) {
91 logger.warn("OSDD path is not a file: {}", path);
92 return null;
93 }
94 return osddFile;
95 }
96
97
98
99
100
101
102 protected boolean isOsddLinkEnabled() {
103 final FessConfig fessConfig = ComponentUtil.getFessConfig();
104 final String osddLinkEnabled = fessConfig.getOsddLinkEnabled();
105 if (Constants.TRUE.equalsIgnoreCase(osddLinkEnabled)) {
106 return true;
107 }
108
109 if (!Constants.AUTO.equalsIgnoreCase(osddLinkEnabled)) {
110 return false;
111 }
112
113 final String ssoType = fessConfig.getSsoType();
114 return StringUtil.isBlank(ssoType) || Constants.NONE.equalsIgnoreCase(ssoType);
115 }
116
117
118
119
120
121
122 public boolean hasOpenSearchFile() {
123 return osddFile != null;
124 }
125
126
127
128
129
130
131 public StreamResponse asStream() {
132 if (osddFile == null) {
133 throw ComponentUtil.getResponseManager().new404("Unsupported Open Search Description Document response.");
134 }
135
136 return new StreamResponse(osddFile.getName()).contentType(contentType + "; charset=" + encoding).stream(out -> {
137 try (InputStream ins = new FileInputStream(osddFile)) {
138 out.write(ins);
139 }
140 });
141 }
142
143
144
145
146
147
148 public void setOsddPath(final String osddPath) {
149 this.osddPath = osddPath;
150 }
151
152
153
154
155
156
157 public void setEncoding(final String encoding) {
158 this.encoding = encoding;
159 }
160
161
162
163
164
165
166 public void setContentType(final String contentType) {
167 this.contentType = contentType;
168 }
169 }