1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.hdfs.web;
19 
20 import java.io.IOException;
21 import java.net.HttpURLConnection;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.security.PrivilegedExceptionAction;
26 import java.util.Map;
27 
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FSDataOutputStream;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.hdfs.DFSConfigKeys;
35 import org.apache.hadoop.hdfs.web.resources.HttpOpParam;
36 import org.apache.hadoop.hdfs.web.resources.Param;
37 import org.apache.hadoop.security.UserGroupInformation;
38 import org.junit.Assert;
39 
40 public class WebHdfsTestUtil {
41   public static final Log LOG = LogFactory.getLog(WebHdfsTestUtil.class);
42 
createConf()43   public static Configuration createConf() {
44     final Configuration conf = new Configuration();
45     conf.setBoolean(DFSConfigKeys.DFS_WEBHDFS_ENABLED_KEY, true);
46     return conf;
47   }
48 
getWebHdfsFileSystem( final Configuration conf, String scheme)49   public static WebHdfsFileSystem getWebHdfsFileSystem(
50       final Configuration conf, String scheme) throws IOException,
51       URISyntaxException {
52     final String uri;
53 
54     if (WebHdfsFileSystem.SCHEME.equals(scheme)) {
55       uri = WebHdfsFileSystem.SCHEME + "://"
56           + conf.get(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY);
57     } else if (SWebHdfsFileSystem.SCHEME.equals(scheme)) {
58       uri = SWebHdfsFileSystem.SCHEME + "://"
59           + conf.get(DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY);
60     } else {
61       throw new IllegalArgumentException("unknown scheme:" + scheme);
62     }
63     return (WebHdfsFileSystem)FileSystem.get(new URI(uri), conf);
64   }
65 
getWebHdfsFileSystemAs( final UserGroupInformation ugi, final Configuration conf )66   public static WebHdfsFileSystem getWebHdfsFileSystemAs(
67   final UserGroupInformation ugi, final Configuration conf
68   ) throws IOException, InterruptedException {
69     return getWebHdfsFileSystemAs(ugi, conf, WebHdfsFileSystem.SCHEME);
70   }
71 
getWebHdfsFileSystemAs( final UserGroupInformation ugi, final Configuration conf, String scheme )72   public static WebHdfsFileSystem getWebHdfsFileSystemAs(
73       final UserGroupInformation ugi, final Configuration conf, String scheme
74       ) throws IOException, InterruptedException {
75     return ugi.doAs(new PrivilegedExceptionAction<WebHdfsFileSystem>() {
76       @Override
77       public WebHdfsFileSystem run() throws Exception {
78         return getWebHdfsFileSystem(conf, WebHdfsFileSystem.SCHEME);
79       }
80     });
81   }
82 
83   public static URL toUrl(final WebHdfsFileSystem webhdfs,
84       final HttpOpParam.Op op, final Path fspath,
85       final Param<?,?>... parameters) throws IOException {
86     final URL url = webhdfs.toUrl(op, fspath, parameters);
87     WebHdfsTestUtil.LOG.info("url=" + url);
88     return url;
89   }
90 
91   public static Map<?, ?> connectAndGetJson(final HttpURLConnection conn,
92       final int expectedResponseCode) throws IOException {
93     conn.connect();
94     Assert.assertEquals(expectedResponseCode, conn.getResponseCode());
95     return WebHdfsFileSystem.jsonParse(conn, false);
96   }
97 }
98