View Javadoc
1   /*
2    * Copyright 2012-2021 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.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   * Helper class for Open Search Description Document.
34   *
35   * @author shinsuke
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"; // "application/opensearchdescription+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  }