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 javax.annotation.PostConstruct;
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.codelibs.core.lang.StringUtil;
27 import org.codelibs.fess.Constants;
28 import org.codelibs.fess.util.ComponentUtil;
29 import org.lastaflute.web.response.StreamResponse;
30 import org.lastaflute.web.util.LaServletContextUtil;
31
32
33
34
35
36
37
38 public class OpenSearchHelper {
39 private static final Logger logger = LogManager.getLogger(OpenSearchHelper.class);
40
41 protected String osddPath;
42
43 protected String encoding = Constants.UTF_8;
44
45 protected String contentType = "text/xml";
46
47 protected File osddFile;
48
49 @PostConstruct
50 public void init() {
51 if (logger.isDebugEnabled()) {
52 logger.debug("Initialize {}", this.getClass().getSimpleName());
53 }
54 if (StringUtil.isNotBlank(osddPath)) {
55 final String path = LaServletContextUtil.getServletContext().getRealPath(osddPath);
56 osddFile = new File(path);
57 if (!osddFile.isFile()) {
58 osddFile = null;
59 logger.warn("{} was not found.", path);
60 }
61 } else {
62 logger.info("OSDD file is not found.");
63 }
64 }
65
66 public boolean hasOpenSearchFile() {
67 return osddFile != null;
68 }
69
70 public StreamResponse asStream() {
71 if (osddFile == null) {
72 throw ComponentUtil.getResponseManager().new404("Unsupported OpenSearch response.");
73 }
74
75 return new StreamResponse(osddFile.getName()).contentType(contentType + "; charset=" + encoding).stream(out -> {
76 try (InputStream ins = new FileInputStream(osddFile)) {
77 out.write(ins);
78 }
79 });
80 }
81
82 public void setOsddPath(final String osddPath) {
83 this.osddPath = osddPath;
84 }
85
86 public void setEncoding(final String encoding) {
87 this.encoding = encoding;
88 }
89
90 public void setContentType(final String contentType) {
91 this.contentType = contentType;
92 }
93 }