1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#include "Exception.h"
29#include "ExceptionInternal.h"
30#include "FileSystemKey.h"
31
32#include <algorithm>
33#include <libxml/uri.h>
34#include <sstream>
35
36namespace Hdfs {
37namespace Internal {
38
39FileSystemKey::FileSystemKey(const std::string & uri, const char * u) {
40 xmlURIPtr uriobj;
41 std::stringstream ss;
42 ss.imbue(std::locale::classic());
43 uriobj = xmlParseURI(uri.c_str());
44
45 try {
46 if (!uriobj || uriobj->server == NULL || 0 == strlen(uriobj->server)) {
47 THROW(InvalidParameter,
48 "Invalid input: uri: %s is not a valid URI type.", uri.c_str());
49 }
50
51 host = uriobj->server;
52
53 if (NULL == uriobj->scheme || 0 == strlen(uriobj->scheme)) {
54 scheme = "hdfs";
55 } else {
56 scheme = uriobj->scheme;
57 }
58
59 if (strcasecmp(scheme.c_str(), "hdfs")) {
60 THROW(InvalidParameter,
61 "Invalid input: uri is not a valid URI type.");
62 }
63
64 if (u && strlen(u) > 0) {
65 user = UserInfo(u);
66 } else if (NULL == uriobj->user || 0 == strlen(uriobj->user)) {
67 user = UserInfo::LocalUser();
68 } else {
69 user = UserInfo(uriobj->user);
70 }
71
72 ss << user.getEffectiveUser();
73
74 if (uriobj->port == 0) {
75 ss << "@" << uriobj->server;
76 } else {
77 std::stringstream s;
78 s.imbue(std::locale::classic());
79 s << uriobj->port;
80 port = s.str();
81 ss << "@" << uriobj->server << ":" << uriobj->port;
82 }
83
84 authority = ss.str();
85 } catch (...) {
86 if (uriobj) {
87 xmlFreeURI(uriobj);
88 }
89
90 throw;
91 }
92
93 xmlFreeURI(uriobj);
94 std::transform(authority.begin(), authority.end(), authority.begin(), tolower);
95 std::transform(scheme.begin(), scheme.end(), scheme.begin(), tolower);
96}
97
98}
99}
100