View Javadoc
1   /*
2    * Copyright 2012-2025 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 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   * Helper class for Open Search Description Document.
35   */
36  public class OsddHelper {
37  
38      /**
39       * Default constructor.
40       */
41      public OsddHelper() {
42          // Default constructor
43      }
44  
45      private static final Logger logger = LogManager.getLogger(OsddHelper.class);
46  
47      /** The OSDD file path. */
48      protected String osddPath;
49  
50      /** The encoding for OSDD file. */
51      protected String encoding = Constants.UTF_8;
52  
53      /** The content type for OSDD response. */
54      protected String contentType = "text/xml"; // "application/opensearchdescription+xml"
55  
56      /** The OSDD file. */
57      protected File osddFile;
58  
59      /**
60       * Initializes the OSDD helper.
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       * Gets the OSDD file.
72       *
73       * @return the OSDD file
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       * Checks if OSDD link is enabled.
99       *
100      * @return true if OSDD link is enabled
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      * Checks if the OpenSearch file exists.
119      *
120      * @return true if the OpenSearch file exists
121      */
122     public boolean hasOpenSearchFile() {
123         return osddFile != null;
124     }
125 
126     /**
127      * Returns the OSDD as a stream response.
128      *
129      * @return the stream response
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      * Sets the OSDD path.
145      *
146      * @param osddPath the OSDD path
147      */
148     public void setOsddPath(final String osddPath) {
149         this.osddPath = osddPath;
150     }
151 
152     /**
153      * Sets the encoding.
154      *
155      * @param encoding the encoding
156      */
157     public void setEncoding(final String encoding) {
158         this.encoding = encoding;
159     }
160 
161     /**
162      * Sets the content type.
163      *
164      * @param contentType the content type
165      */
166     public void setContentType(final String contentType) {
167         this.contentType = contentType;
168     }
169 }