View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.servlets;
20  
21  import java.net.InetAddress;
22  
23  import org.mycore.common.config.MCRConfigurationException;
24  import org.mycore.frontend.MCRFrontendUtil;
25  
26  import jakarta.servlet.http.HttpServletRequest;
27  import jakarta.servlet.http.HttpServletResponse;
28  
29  /**
30   * This class simply is a container for objects needed during a Servlet session
31   * like the HttpServletRequest and HttpServeltResponse. The class provids only
32   * get-methods to return the objects set while constructing the job object.
33   * 
34   * @author Detlev Degenhardt
35   * @version $Revision$ $Date$
36   */
37  public class MCRServletJob {
38      /** The HttpServletRequest object */
39      private HttpServletRequest theRequest = null;
40  
41      /** The HttpServletResponse object */
42      private HttpServletResponse theResponse = null;
43  
44      /**
45       * The constructor takes the given objects and stores them in private
46       * objects.
47       * 
48       * @param theRequest
49       *            the HttpServletRequest object for this servlet job
50       * @param theResponse
51       *            the HttpServletResponse object for this servlet job
52       */
53      public MCRServletJob(HttpServletRequest theRequest, HttpServletResponse theResponse) {
54          this.theRequest = theRequest;
55          this.theResponse = theResponse;
56      }
57  
58      /** returns the HttpServletRequest object */
59      public HttpServletRequest getRequest() {
60          return theRequest;
61      }
62  
63      /** returns the HttpServletResponse object */
64      public HttpServletResponse getResponse() {
65          return theResponse;
66      }
67  
68      /** returns true if the current http request was issued from the local host * */
69      public boolean isLocal() {
70          try {
71              String serverName = theRequest.getServerName();
72              String serverIP = InetAddress.getByName(serverName).getHostAddress();
73              String remoteIP = MCRFrontendUtil.getRemoteAddr(theRequest);
74  
75              return remoteIP.equals(serverIP) || remoteIP.equals("127.0.0.1");
76          } catch (Exception ex) {
77              String msg = "Exception while testing if http request was from local host";
78              throw new MCRConfigurationException(msg, ex);
79          }
80      }
81  }