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 import org.apache.hadoop.conf.Configuration;
022 import org.apache.hadoop.fs.FileSystem;
023 import org.apache.hadoop.fs.http.client.HttpFSFileSystem;
024 import org.apache.hadoop.fs.http.server.HttpFSParams.AccessTimeParam;
025 import org.apache.hadoop.fs.http.server.HttpFSParams.BlockSizeParam;
026 import org.apache.hadoop.fs.http.server.HttpFSParams.DataParam;
027 import org.apache.hadoop.fs.http.server.HttpFSParams.DeleteOpParam;
028 import org.apache.hadoop.fs.http.server.HttpFSParams.DeleteRecursiveParam;
029 import org.apache.hadoop.fs.http.server.HttpFSParams.DoAsParam;
030 import org.apache.hadoop.fs.http.server.HttpFSParams.FilterParam;
031 import org.apache.hadoop.fs.http.server.HttpFSParams.FsPathParam;
032 import org.apache.hadoop.fs.http.server.HttpFSParams.GetOpParam;
033 import org.apache.hadoop.fs.http.server.HttpFSParams.GroupParam;
034 import org.apache.hadoop.fs.http.server.HttpFSParams.LenParam;
035 import org.apache.hadoop.fs.http.server.HttpFSParams.ModifiedTimeParam;
036 import org.apache.hadoop.fs.http.server.HttpFSParams.OffsetParam;
037 import org.apache.hadoop.fs.http.server.HttpFSParams.OverwriteParam;
038 import org.apache.hadoop.fs.http.server.HttpFSParams.OwnerParam;
039 import org.apache.hadoop.fs.http.server.HttpFSParams.PermissionParam;
040 import org.apache.hadoop.fs.http.server.HttpFSParams.PostOpParam;
041 import org.apache.hadoop.fs.http.server.HttpFSParams.PutOpParam;
042 import org.apache.hadoop.fs.http.server.HttpFSParams.ReplicationParam;
043 import org.apache.hadoop.fs.http.server.HttpFSParams.ToPathParam;
044 import org.apache.hadoop.lib.service.FileSystemAccess;
045 import org.apache.hadoop.lib.service.FileSystemAccessException;
046 import org.apache.hadoop.lib.service.Groups;
047 import org.apache.hadoop.lib.service.Instrumentation;
048 import org.apache.hadoop.lib.service.ProxyUser;
049 import org.apache.hadoop.lib.servlet.FileSystemReleaseFilter;
050 import org.apache.hadoop.lib.servlet.HostnameFilter;
051 import org.apache.hadoop.lib.wsrs.InputStreamEntity;
052 import org.apache.hadoop.security.authentication.server.AuthenticationToken;
053 import org.json.simple.JSONObject;
054 import org.slf4j.Logger;
055 import org.slf4j.LoggerFactory;
056 import org.slf4j.MDC;
057
058 import javax.ws.rs.Consumes;
059 import javax.ws.rs.DELETE;
060 import javax.ws.rs.DefaultValue;
061 import javax.ws.rs.GET;
062 import javax.ws.rs.POST;
063 import javax.ws.rs.PUT;
064 import javax.ws.rs.Path;
065 import javax.ws.rs.PathParam;
066 import javax.ws.rs.Produces;
067 import javax.ws.rs.QueryParam;
068 import javax.ws.rs.core.Context;
069 import javax.ws.rs.core.MediaType;
070 import javax.ws.rs.core.Response;
071 import javax.ws.rs.core.UriBuilder;
072 import javax.ws.rs.core.UriInfo;
073 import java.io.IOException;
074 import java.io.InputStream;
075 import java.net.URI;
076 import java.security.AccessControlException;
077 import java.security.Principal;
078 import java.text.MessageFormat;
079 import java.util.List;
080 import java.util.Map;
081
082 /**
083 * Main class of HttpFSServer server.
084 * <p/>
085 * The <code>HttpFSServer</code> class uses Jersey JAX-RS to binds HTTP requests to the
086 * different operations.
087 */
088 @Path(HttpFSFileSystem.SERVICE_VERSION)
089 public class HttpFSServer {
090 private static Logger AUDIT_LOG = LoggerFactory.getLogger("httpfsaudit");
091
092 /**
093 * Special binding for '/' as it is not handled by the wildcard binding.
094 *
095 * @param user principal making the request.
096 * @param op GET operation, default value is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#OPEN}.
097 * @param filter Glob filter, default value is none. Used only if the
098 * operation is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#LISTSTATUS}
099 * @param doAs user being impersonated, defualt value is none. It can be used
100 * only if the current user is a HttpFSServer proxyuser.
101 *
102 * @return the request response
103 *
104 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
105 * handled by {@link HttpFSExceptionProvider}.
106 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
107 * exceptions are handled by {@link HttpFSExceptionProvider}.
108 */
109 @GET
110 @Path("/")
111 @Produces(MediaType.APPLICATION_JSON)
112 public Response root(@Context Principal user,
113 @QueryParam(GetOpParam.NAME) GetOpParam op,
114 @QueryParam(FilterParam.NAME) @DefaultValue(FilterParam.DEFAULT) FilterParam filter,
115 @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) DoAsParam doAs)
116 throws IOException, FileSystemAccessException {
117 return get(user, new FsPathParam(""), op, new OffsetParam(OffsetParam.DEFAULT),
118 new LenParam(LenParam.DEFAULT), filter, doAs,
119 new OverwriteParam(OverwriteParam.DEFAULT),
120 new BlockSizeParam(BlockSizeParam.DEFAULT),
121 new PermissionParam(PermissionParam.DEFAULT),
122 new ReplicationParam(ReplicationParam.DEFAULT));
123 }
124
125 /**
126 * Resolves the effective user that will be used to request a FileSystemAccess filesystem.
127 * <p/>
128 * If the doAs-user is NULL or the same as the user, it returns the user.
129 * <p/>
130 * Otherwise it uses proxyuser rules (see {@link ProxyUser} to determine if the
131 * current user can impersonate the doAs-user.
132 * <p/>
133 * If the current user cannot impersonate the doAs-user an
134 * <code>AccessControlException</code> will be thrown.
135 *
136 * @param user principal for whom the filesystem instance is.
137 * @param doAs do-as user, if any.
138 *
139 * @return the effective user.
140 *
141 * @throws IOException thrown if an IO error occurrs.
142 * @throws AccessControlException thrown if the current user cannot impersonate
143 * the doAs-user.
144 */
145 private String getEffectiveUser(Principal user, String doAs) throws IOException {
146 String effectiveUser = user.getName();
147 if (doAs != null && !doAs.equals(user.getName())) {
148 ProxyUser proxyUser = HttpFSServerWebApp.get().get(ProxyUser.class);
149 String proxyUserName;
150 if (user instanceof AuthenticationToken) {
151 proxyUserName = ((AuthenticationToken)user).getUserName();
152 } else {
153 proxyUserName = user.getName();
154 }
155 proxyUser.validate(proxyUserName, HostnameFilter.get(), doAs);
156 effectiveUser = doAs;
157 AUDIT_LOG.info("Proxy user [{}] DoAs user [{}]", proxyUserName, doAs);
158 }
159 return effectiveUser;
160 }
161
162 /**
163 * Executes a {@link FileSystemAccess.FileSystemExecutor} using a filesystem for the effective
164 * user.
165 *
166 * @param user principal making the request.
167 * @param doAs do-as user, if any.
168 * @param executor FileSystemExecutor to execute.
169 *
170 * @return FileSystemExecutor response
171 *
172 * @throws IOException thrown if an IO error occurrs.
173 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
174 * exceptions are handled by {@link HttpFSExceptionProvider}.
175 */
176 private <T> T fsExecute(Principal user, String doAs, FileSystemAccess.FileSystemExecutor<T> executor)
177 throws IOException, FileSystemAccessException {
178 String hadoopUser = getEffectiveUser(user, doAs);
179 FileSystemAccess fsAccess = HttpFSServerWebApp.get().get(FileSystemAccess.class);
180 Configuration conf = HttpFSServerWebApp.get().get(FileSystemAccess.class).getFileSystemConfiguration();
181 return fsAccess.execute(hadoopUser, conf, executor);
182 }
183
184 /**
185 * Returns a filesystem instance. The fileystem instance is wired for release at the completion of
186 * the current Servlet request via the {@link FileSystemReleaseFilter}.
187 * <p/>
188 * If a do-as user is specified, the current user must be a valid proxyuser, otherwise an
189 * <code>AccessControlException</code> will be thrown.
190 *
191 * @param user principal for whom the filesystem instance is.
192 * @param doAs do-as user, if any.
193 *
194 * @return a filesystem for the specified user or do-as user.
195 *
196 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
197 * handled by {@link HttpFSExceptionProvider}.
198 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
199 * exceptions are handled by {@link HttpFSExceptionProvider}.
200 */
201 private FileSystem createFileSystem(Principal user, String doAs) throws IOException, FileSystemAccessException {
202 String hadoopUser = getEffectiveUser(user, doAs);
203 FileSystemAccess fsAccess = HttpFSServerWebApp.get().get(FileSystemAccess.class);
204 Configuration conf = HttpFSServerWebApp.get().get(FileSystemAccess.class).getFileSystemConfiguration();
205 FileSystem fs = fsAccess.createFileSystem(hadoopUser, conf);
206 FileSystemReleaseFilter.setFileSystem(fs);
207 return fs;
208 }
209
210 /**
211 * Binding to handle all GET requests, supported operations are
212 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues}.
213 * <p/>
214 * The @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#INSTRUMENTATION} operation is available only
215 * to users that are in HttpFSServer's admin group (see {@link HttpFSServer}. It returns
216 * HttpFSServer instrumentation data. The specified path must be '/'.
217 *
218 * @param user principal making the request.
219 * @param path path for the GET request.
220 * @param op GET operation, default value is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#OPEN}.
221 * @param offset of the file being fetch, used only with
222 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#OPEN} operations.
223 * @param len amounts of bytes, used only with @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#OPEN}
224 * operations.
225 * @param filter Glob filter, default value is none. Used only if the
226 * operation is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.GetOpValues#LISTSTATUS}
227 * @param doAs user being impersonated, defualt value is none. It can be used
228 * only if the current user is a HttpFSServer proxyuser.
229 * @param override default is true. Used only for
230 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#CREATE} operations.
231 * @param blockSize block size to set, used only by
232 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#CREATE} operations.
233 * @param permission permission to set, used only by
234 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETPERMISSION}.
235 * @param replication replication factor to set, used only by
236 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETREPLICATION}.
237 *
238 * @return the request response.
239 *
240 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
241 * handled by {@link HttpFSExceptionProvider}.
242 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
243 * exceptions are handled by {@link HttpFSExceptionProvider}.
244 */
245 @GET
246 @Path("{path:.*}")
247 @Produces({MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON})
248 public Response get(@Context Principal user,
249 @PathParam("path") @DefaultValue("") FsPathParam path,
250 @QueryParam(GetOpParam.NAME) GetOpParam op,
251 @QueryParam(OffsetParam.NAME) @DefaultValue(OffsetParam.DEFAULT) OffsetParam offset,
252 @QueryParam(LenParam.NAME) @DefaultValue(LenParam.DEFAULT) LenParam len,
253 @QueryParam(FilterParam.NAME) @DefaultValue(FilterParam.DEFAULT) FilterParam filter,
254 @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) DoAsParam doAs,
255
256 //these params are only for createHandle operation acceptance purposes
257 @QueryParam(OverwriteParam.NAME) @DefaultValue(OverwriteParam.DEFAULT) OverwriteParam override,
258 @QueryParam(BlockSizeParam.NAME) @DefaultValue(BlockSizeParam.DEFAULT) BlockSizeParam blockSize,
259 @QueryParam(PermissionParam.NAME) @DefaultValue(PermissionParam.DEFAULT)
260 PermissionParam permission,
261 @QueryParam(ReplicationParam.NAME) @DefaultValue(ReplicationParam.DEFAULT)
262 ReplicationParam replication
263 )
264 throws IOException, FileSystemAccessException {
265 Response response = null;
266 if (op == null) {
267 throw new UnsupportedOperationException(MessageFormat.format("Missing [{0}] parameter", GetOpParam.NAME));
268 } else {
269 path.makeAbsolute();
270 MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name());
271 switch (op.value()) {
272 case OPEN: {
273 //Invoking the command directly using an unmanaged FileSystem that is released by the
274 //FileSystemReleaseFilter
275 FSOperations.FSOpen command = new FSOperations.FSOpen(path.value());
276 FileSystem fs = createFileSystem(user, doAs.value());
277 InputStream is = command.execute(fs);
278 AUDIT_LOG.info("[{}] offset [{}] len [{}]", new Object[]{path, offset, len});
279 InputStreamEntity entity = new InputStreamEntity(is, offset.value(), len.value());
280 response = Response.ok(entity).type(MediaType.APPLICATION_OCTET_STREAM).build();
281 break;
282 }
283 case GETFILESTATUS: {
284 FSOperations.FSFileStatus command = new FSOperations.FSFileStatus(path.value());
285 Map json = fsExecute(user, doAs.value(), command);
286 AUDIT_LOG.info("[{}]", path);
287 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
288 break;
289 }
290 case LISTSTATUS: {
291 FSOperations.FSListStatus command = new FSOperations.FSListStatus(path.value(), filter.value());
292 Map json = fsExecute(user, doAs.value(), command);
293 if (filter.value() == null) {
294 AUDIT_LOG.info("[{}]", path);
295 } else {
296 AUDIT_LOG.info("[{}] filter [{}]", path, filter.value());
297 }
298 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
299 break;
300 }
301 case GETHOMEDIRECTORY: {
302 FSOperations.FSHomeDir command = new FSOperations.FSHomeDir();
303 JSONObject json = fsExecute(user, doAs.value(), command);
304 AUDIT_LOG.info("");
305 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
306 break;
307 }
308 case INSTRUMENTATION: {
309 if (!path.value().equals("/")) {
310 throw new UnsupportedOperationException(
311 MessageFormat.format("Invalid path for {0}={1}, must be '/'",
312 GetOpParam.NAME, HttpFSFileSystem.GetOpValues.INSTRUMENTATION));
313 }
314 Groups groups = HttpFSServerWebApp.get().get(Groups.class);
315 List<String> userGroups = groups.getGroups(user.getName());
316 if (!userGroups.contains(HttpFSServerWebApp.get().getAdminGroup())) {
317 throw new AccessControlException("User not in HttpFSServer admin group");
318 }
319 Instrumentation instrumentation = HttpFSServerWebApp.get().get(Instrumentation.class);
320 Map snapshot = instrumentation.getSnapshot();
321 response = Response.ok(snapshot).build();
322 break;
323 }
324 case GETCONTENTSUMMARY: {
325 FSOperations.FSContentSummary command = new FSOperations.FSContentSummary(path.value());
326 Map json = fsExecute(user, doAs.value(), command);
327 AUDIT_LOG.info("[{}]", path);
328 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
329 break;
330 }
331 case GETFILECHECKSUM: {
332 FSOperations.FSFileChecksum command = new FSOperations.FSFileChecksum(path.value());
333 Map json = fsExecute(user, doAs.value(), command);
334 AUDIT_LOG.info("[{}]", path);
335 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
336 break;
337 }
338 case GETDELEGATIONTOKEN: {
339 response = Response.status(Response.Status.BAD_REQUEST).build();
340 break;
341 }
342 case GETFILEBLOCKLOCATIONS: {
343 response = Response.status(Response.Status.BAD_REQUEST).build();
344 break;
345 }
346 }
347 return response;
348 }
349 }
350
351 /**
352 * Creates the URL for an upload operation (create or append).
353 *
354 * @param uriInfo uri info of the request.
355 * @param uploadOperation operation for the upload URL.
356 *
357 * @return the URI for uploading data.
358 */
359 protected URI createUploadRedirectionURL(UriInfo uriInfo, Enum<?> uploadOperation) {
360 UriBuilder uriBuilder = uriInfo.getRequestUriBuilder();
361 uriBuilder = uriBuilder.replaceQueryParam(PutOpParam.NAME, uploadOperation).
362 queryParam(DataParam.NAME, Boolean.TRUE);
363 return uriBuilder.build(null);
364 }
365
366 /**
367 * Binding to handle all DELETE requests.
368 *
369 * @param user principal making the request.
370 * @param path path for the DELETE request.
371 * @param op DELETE operation, default value is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.DeleteOpValues#DELETE}.
372 * @param recursive indicates if the delete is recursive, default is <code>false</code>
373 * @param doAs user being impersonated, defualt value is none. It can be used
374 * only if the current user is a HttpFSServer proxyuser.
375 *
376 * @return the request response.
377 *
378 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
379 * handled by {@link HttpFSExceptionProvider}.
380 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
381 * exceptions are handled by {@link HttpFSExceptionProvider}.
382 */
383 @DELETE
384 @Path("{path:.*}")
385 @Produces(MediaType.APPLICATION_JSON)
386 public Response delete(@Context Principal user,
387 @PathParam("path") FsPathParam path,
388 @QueryParam(DeleteOpParam.NAME) DeleteOpParam op,
389 @QueryParam(DeleteRecursiveParam.NAME) @DefaultValue(DeleteRecursiveParam.DEFAULT)
390 DeleteRecursiveParam recursive,
391 @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) DoAsParam doAs)
392 throws IOException, FileSystemAccessException {
393 Response response = null;
394 if (op == null) {
395 throw new UnsupportedOperationException(MessageFormat.format("Missing [{0}] parameter", DeleteOpParam.NAME));
396 }
397 switch (op.value()) {
398 case DELETE: {
399 path.makeAbsolute();
400 MDC.put(HttpFSFileSystem.OP_PARAM, "DELETE");
401 AUDIT_LOG.info("[{}] recursive [{}]", path, recursive);
402 FSOperations.FSDelete command = new FSOperations.FSDelete(path.value(), recursive.value());
403 JSONObject json = fsExecute(user, doAs.value(), command);
404 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
405 break;
406 }
407 }
408 return response;
409 }
410
411
412 /**
413 * Binding to handle all PUT requests, supported operations are
414 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues}.
415 *
416 * @param is request input stream, used only for
417 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PostOpValues#APPEND} operations.
418 * @param user principal making the request.
419 * @param uriInfo the request uriInfo.
420 * @param path path for the PUT request.
421 * @param op PUT operation, no default value.
422 * @param toPath new path, used only for
423 * {@link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#RENAME} operations.
424 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETTIMES}.
425 * @param owner owner to set, used only for
426 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETOWNER} operations.
427 * @param group group to set, used only for
428 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETOWNER} operations.
429 * @param override default is true. Used only for
430 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#CREATE} operations.
431 * @param blockSize block size to set, used only by
432 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#CREATE} operations.
433 * @param permission permission to set, used only by
434 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETPERMISSION}.
435 * @param replication replication factor to set, used only by
436 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETREPLICATION}.
437 * @param modifiedTime modified time, in seconds since EPOC, used only by
438 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETTIMES}.
439 * @param accessTime accessed time, in seconds since EPOC, used only by
440 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PutOpValues#SETTIMES}.
441 * @param hasData indicates if the append request is uploading data or not
442 * (just getting the handle).
443 * @param doAs user being impersonated, defualt value is none. It can be used
444 * only if the current user is a HttpFSServer proxyuser.
445 *
446 * @return the request response.
447 *
448 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
449 * handled by {@link HttpFSExceptionProvider}.
450 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
451 * exceptions are handled by {@link HttpFSExceptionProvider}.
452 */
453 @PUT
454 @Path("{path:.*}")
455 @Consumes({"*/*"})
456 @Produces({MediaType.APPLICATION_JSON})
457 public Response put(InputStream is,
458 @Context Principal user,
459 @Context UriInfo uriInfo,
460 @PathParam("path") FsPathParam path,
461 @QueryParam(PutOpParam.NAME) PutOpParam op,
462 @QueryParam(ToPathParam.NAME) @DefaultValue(ToPathParam.DEFAULT) ToPathParam toPath,
463 @QueryParam(OwnerParam.NAME) @DefaultValue(OwnerParam.DEFAULT) OwnerParam owner,
464 @QueryParam(GroupParam.NAME) @DefaultValue(GroupParam.DEFAULT) GroupParam group,
465 @QueryParam(OverwriteParam.NAME) @DefaultValue(OverwriteParam.DEFAULT) OverwriteParam override,
466 @QueryParam(BlockSizeParam.NAME) @DefaultValue(BlockSizeParam.DEFAULT) BlockSizeParam blockSize,
467 @QueryParam(PermissionParam.NAME) @DefaultValue(PermissionParam.DEFAULT)
468 PermissionParam permission,
469 @QueryParam(ReplicationParam.NAME) @DefaultValue(ReplicationParam.DEFAULT)
470 ReplicationParam replication,
471 @QueryParam(ModifiedTimeParam.NAME) @DefaultValue(ModifiedTimeParam.DEFAULT)
472 ModifiedTimeParam modifiedTime,
473 @QueryParam(AccessTimeParam.NAME) @DefaultValue(AccessTimeParam.DEFAULT)
474 AccessTimeParam accessTime,
475 @QueryParam(DataParam.NAME) @DefaultValue(DataParam.DEFAULT) DataParam hasData,
476 @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) DoAsParam doAs)
477 throws IOException, FileSystemAccessException {
478 Response response = null;
479 if (op == null) {
480 throw new UnsupportedOperationException(MessageFormat.format("Missing [{0}] parameter", PutOpParam.NAME));
481 }
482 path.makeAbsolute();
483 MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name());
484 switch (op.value()) {
485 case CREATE: {
486 if (!hasData.value()) {
487 response = Response.temporaryRedirect(
488 createUploadRedirectionURL(uriInfo, HttpFSFileSystem.PutOpValues.CREATE)).build();
489 } else {
490 FSOperations.FSCreate
491 command = new FSOperations.FSCreate(is, path.value(), permission.value(), override.value(),
492 replication.value(), blockSize.value());
493 fsExecute(user, doAs.value(), command);
494 AUDIT_LOG.info("[{}] permission [{}] override [{}] replication [{}] blockSize [{}]",
495 new Object[]{path, permission, override, replication, blockSize});
496 response = Response.status(Response.Status.CREATED).build();
497 }
498 break;
499 }
500 case MKDIRS: {
501 FSOperations.FSMkdirs command = new FSOperations.FSMkdirs(path.value(), permission.value());
502 JSONObject json = fsExecute(user, doAs.value(), command);
503 AUDIT_LOG.info("[{}] permission [{}]", path, permission.value());
504 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
505 break;
506 }
507 case RENAME: {
508 FSOperations.FSRename command = new FSOperations.FSRename(path.value(), toPath.value());
509 JSONObject json = fsExecute(user, doAs.value(), command);
510 AUDIT_LOG.info("[{}] to [{}]", path, toPath);
511 response = Response.ok(json).type(MediaType.APPLICATION_JSON).build();
512 break;
513 }
514 case SETOWNER: {
515 FSOperations.FSSetOwner command = new FSOperations.FSSetOwner(path.value(), owner.value(), group.value());
516 fsExecute(user, doAs.value(), command);
517 AUDIT_LOG.info("[{}] to (O/G)[{}]", path, owner.value() + ":" + group.value());
518 response = Response.ok().build();
519 break;
520 }
521 case SETPERMISSION: {
522 FSOperations.FSSetPermission command = new FSOperations.FSSetPermission(path.value(), permission.value());
523 fsExecute(user, doAs.value(), command);
524 AUDIT_LOG.info("[{}] to [{}]", path, permission.value());
525 response = Response.ok().build();
526 break;
527 }
528 case SETREPLICATION: {
529 FSOperations.FSSetReplication command = new FSOperations.FSSetReplication(path.value(), replication.value());
530 JSONObject json = fsExecute(user, doAs.value(), command);
531 AUDIT_LOG.info("[{}] to [{}]", path, replication.value());
532 response = Response.ok(json).build();
533 break;
534 }
535 case SETTIMES: {
536 FSOperations.FSSetTimes
537 command = new FSOperations.FSSetTimes(path.value(), modifiedTime.value(), accessTime.value());
538 fsExecute(user, doAs.value(), command);
539 AUDIT_LOG.info("[{}] to (M/A)[{}]", path, modifiedTime.value() + ":" + accessTime.value());
540 response = Response.ok().build();
541 break;
542 }
543 case RENEWDELEGATIONTOKEN: {
544 response = Response.status(Response.Status.BAD_REQUEST).build();
545 break;
546 }
547 case CANCELDELEGATIONTOKEN: {
548 response = Response.status(Response.Status.BAD_REQUEST).build();
549 break;
550 }
551 }
552 return response;
553 }
554
555 /**
556 * Binding to handle all OPST requests, supported operations are
557 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PostOpValues}.
558 *
559 * @param is request input stream, used only for
560 * @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PostOpValues#APPEND} operations.
561 * @param user principal making the request.
562 * @param uriInfo the request uriInfo.
563 * @param path path for the POST request.
564 * @param op POST operation, default is @link org.apache.hadoop.fs.http.client.HttpFSFileSystem.PostOpValues#APPEND}.
565 * @param hasData indicates if the append request is uploading data or not (just getting the handle).
566 * @param doAs user being impersonated, defualt value is none. It can be used
567 * only if the current user is a HttpFSServer proxyuser.
568 *
569 * @return the request response.
570 *
571 * @throws IOException thrown if an IO error occurred. Thrown exceptions are
572 * handled by {@link HttpFSExceptionProvider}.
573 * @throws FileSystemAccessException thrown if a FileSystemAccess releated error occurred. Thrown
574 * exceptions are handled by {@link HttpFSExceptionProvider}.
575 */
576 @POST
577 @Path("{path:.*}")
578 @Consumes({"*/*"})
579 @Produces({MediaType.APPLICATION_JSON})
580 public Response post(InputStream is,
581 @Context Principal user,
582 @Context UriInfo uriInfo,
583 @PathParam("path") FsPathParam path,
584 @QueryParam(PostOpParam.NAME) PostOpParam op,
585 @QueryParam(DataParam.NAME) @DefaultValue(DataParam.DEFAULT) DataParam hasData,
586 @QueryParam(DoAsParam.NAME) @DefaultValue(DoAsParam.DEFAULT) DoAsParam doAs)
587 throws IOException, FileSystemAccessException {
588 Response response = null;
589 if (op == null) {
590 throw new UnsupportedOperationException(MessageFormat.format("Missing [{0}] parameter", PostOpParam.NAME));
591 }
592 path.makeAbsolute();
593 MDC.put(HttpFSFileSystem.OP_PARAM, op.value().name());
594 switch (op.value()) {
595 case APPEND: {
596 if (!hasData.value()) {
597 response = Response.temporaryRedirect(
598 createUploadRedirectionURL(uriInfo, HttpFSFileSystem.PostOpValues.APPEND)).build();
599 } else {
600 FSOperations.FSAppend command = new FSOperations.FSAppend(is, path.value());
601 fsExecute(user, doAs.value(), command);
602 AUDIT_LOG.info("[{}]", path);
603 response = Response.ok().type(MediaType.APPLICATION_JSON).build();
604 }
605 break;
606 }
607 }
608 return response;
609 }
610
611 }