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.classification.InterfaceAudience;
023 import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
024
025 import javax.servlet.Filter;
026 import javax.servlet.FilterChain;
027 import javax.servlet.FilterConfig;
028 import javax.servlet.ServletException;
029 import javax.servlet.ServletRequest;
030 import javax.servlet.ServletResponse;
031 import javax.servlet.http.HttpServletRequest;
032 import javax.servlet.http.HttpServletResponse;
033 import java.io.IOException;
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 @InterfaceAudience.Private
042 public class CheckUploadContentTypeFilter implements Filter {
043
044 private static final Set<String> UPLOAD_OPERATIONS = new HashSet<String>();
045
046 static {
047 UPLOAD_OPERATIONS.add(HttpFSFileSystem.Operation.APPEND.toString());
048 UPLOAD_OPERATIONS.add(HttpFSFileSystem.Operation.CREATE.toString());
049 }
050
051 /**
052 * Initializes the filter.
053 * <p/>
054 * This implementation is a NOP.
055 *
056 * @param config filter configuration.
057 *
058 * @throws ServletException thrown if the filter could not be initialized.
059 */
060 @Override
061 public void init(FilterConfig config) throws ServletException {
062 }
063
064 /**
065 * Enforces the content-type to be application/octet-stream for
066 * POST and PUT requests.
067 *
068 * @param request servlet request.
069 * @param response servlet response.
070 * @param chain filter chain.
071 *
072 * @throws IOException thrown if an IO error occurrs.
073 * @throws ServletException thrown if a servet error occurrs.
074 */
075 @Override
076 public void doFilter(ServletRequest request, ServletResponse response,
077 FilterChain chain)
078 throws IOException, ServletException {
079 boolean contentTypeOK = true;
080 HttpServletRequest httpReq = (HttpServletRequest) request;
081 HttpServletResponse httpRes = (HttpServletResponse) response;
082 String method = httpReq.getMethod();
083 if (method.equals("PUT") || method.equals("POST")) {
084 String op = httpReq.getParameter(HttpFSFileSystem.OP_PARAM);
085 if (op != null && UPLOAD_OPERATIONS.contains(op.toUpperCase())) {
086 if ("true".equalsIgnoreCase(httpReq.getParameter(HttpFSParametersProvider.DataParam.NAME))) {
087 String contentType = httpReq.getContentType();
088 contentTypeOK =
089 HttpFSFileSystem.UPLOAD_CONTENT_TYPE.equalsIgnoreCase(contentType);
090 }
091 }
092 }
093 if (contentTypeOK) {
094 chain.doFilter(httpReq, httpRes);
095 }
096 else {
097 httpRes.sendError(HttpServletResponse.SC_BAD_REQUEST,
098 "Data upload requests must have content-type set to '" +
099 HttpFSFileSystem.UPLOAD_CONTENT_TYPE + "'");
100
101 }
102 }
103
104 /**
105 * Destroys the filter.
106 * <p/>
107 * This implementation is a NOP.
108 */
109 @Override
110 public void destroy() {
111 }
112
113 }