| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| |
| package org.netbeans.modules.httpserver; |
| |
| import java.io.IOException; |
| import java.net.InetAddress; |
| import java.util.Set; |
| import javax.servlet.*; |
| import javax.servlet.http.*; |
| import org.openide.util.Exceptions; |
| |
| import org.openide.util.NbBundle; |
| |
| /** Base servlet for servlets which access NetBeans Open APIs |
| * |
| * @author Petr Jiricka |
| * @version 0.11 May 5, 1999 |
| */ |
| public abstract class NbBaseServlet extends HttpServlet { |
| |
| /** Initializes the servlet. */ |
| public void init() throws ServletException { |
| } |
| |
| /** Processes the request for both HTTP GET and POST methods |
| * @param request servlet request |
| * @param response servlet response |
| */ |
| protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response) |
| throws ServletException, java.io.IOException; |
| |
| /** Performs the HTTP GET operation. |
| * @param request servlet request |
| * @param response servlet response |
| */ |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| throws ServletException, java.io.IOException { |
| processRequest(request, response); |
| } |
| |
| /** Performs the HTTP POST operation. |
| * @param request servlet request |
| * @param response servlet response |
| */ |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| throws ServletException, java.io.IOException { |
| processRequest(request, response); |
| } |
| |
| /** |
| * Returns a short description of the servlet. |
| */ |
| public String getServletInfo() { |
| return NbBundle.getBundle(NbBaseServlet.class).getString("MSG_BaseServletDescr"); |
| } |
| |
| /** Checks whether access should be permitted according to HTTP Server module access settings |
| * (localhost/anyhost, granted addesses) |
| * @return true if access is granted |
| */ |
| protected boolean checkAccess(HttpServletRequest request) throws IOException { |
| |
| HttpServerSettings settings = HttpServerSettings.getDefault(); |
| if (settings == null) |
| return false; |
| |
| if (settings.getHostProperty ().getHost ().equals(HttpServerSettings.ANYHOST)) |
| return true; |
| |
| Set hs = settings.getGrantedAddressesSet(); |
| |
| if (hs.contains(request.getRemoteAddr().trim())) |
| return true; |
| |
| String pathI = request.getPathInfo(); |
| if (pathI == null) |
| pathI = ""; // NOI18N |
| // ask user |
| try { |
| String address = request.getRemoteAddr().trim(); |
| if (settings.allowAccess(InetAddress.getByName(address), pathI)) return true; |
| } catch (Exception ex) { |
| Exceptions.printStackTrace(ex); |
| return false; |
| } |
| |
| return false; |
| } |
| |
| } |