001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.hadoop.fs.http.server;
020
021
022 import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
023
024 import javax.servlet.Filter;
025 import javax.servlet.FilterChain;
026 import javax.servlet.FilterConfig;
027 import javax.servlet.ServletException;
028 import javax.servlet.ServletRequest;
029 import javax.servlet.ServletResponse;
030 import javax.servlet.http.HttpServletRequest;
031 import javax.servlet.http.HttpServletResponse;
032 import java.io.IOException;
033 import java.net.InetAddress;
034 import java.util.HashSet;
035 import java.util.Set;
036
037 /**
038 * Filter that Enforces the content-type to be application/octet-stream for
039 * POST and PUT requests.
040 */
041 public class CheckUploadContentTypeFilter implements Filter {
042
043 private static final Set<String> UPLOAD_OPERATIONS = new HashSet<String>();
044
045 static {
046 UPLOAD_OPERATIONS.add(HttpFSFileSystem.PostOpValues.APPEND.toString());
047 UPLOAD_OPERATIONS.add(HttpFSFileSystem.PutOpValues.CREATE.toString());
048 }
049
050 /**
051 * Initializes the filter.
052 * <p/>
053 * This implementation is a NOP.
054 *
055 * @param config filter configuration.
056 *
057 * @throws ServletException thrown if the filter could not be initialized.
058 */
059 @Override
060 public void init(FilterConfig config) throws ServletException {
061 }
062
063 /**
064 * Enforces the content-type to be application/octet-stream for
065 * POST and PUT requests.
066 *
067 * @param request servlet request.
068 * @param response servlet response.
069 * @param chain filter chain.
070 *
071 * @throws IOException thrown if an IO error occurrs.
072 * @throws ServletException thrown if a servet error occurrs.
073 */
074 @Override
075 public void doFilter(ServletRequest request, ServletResponse response,
076 FilterChain chain)
077 throws IOException, ServletException {
078 boolean contentTypeOK = true;
079 HttpServletRequest httpReq = (HttpServletRequest) request;
080 HttpServletResponse httpRes = (HttpServletResponse) response;
081 String method = httpReq.getMethod();
082 if (method.equals("PUT") || method.equals("POST")) {
083 String op = httpReq.getParameter(HttpFSFileSystem.OP_PARAM);
084 if (op != null && UPLOAD_OPERATIONS.contains(op.toUpperCase())) {
085 if ("true".equalsIgnoreCase(httpReq.getParameter(HttpFSParams.DataParam.NAME))) {
086 String contentType = httpReq.getContentType();
087 contentTypeOK =
088 HttpFSFileSystem.UPLOAD_CONTENT_TYPE.equalsIgnoreCase(contentType);
089 }
090 }
091 }
092 if (contentTypeOK) {
093 chain.doFilter(httpReq, httpRes);
094 }
095 else {
096 httpRes.sendError(HttpServletResponse.SC_BAD_REQUEST,
097 "Data upload requests must have content-type set to '" +
098 HttpFSFileSystem.UPLOAD_CONTENT_TYPE + "'");
099
100 }
101 }
102
103 /**
104 * Destroys the filter.
105 * <p/>
106 * This implementation is a NOP.
107 */
108 @Override
109 public void destroy() {
110 }
111
112 }