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
019package org.apache.hadoop.yarn.api.records;
020
021import java.net.URI;
022import java.net.URISyntaxException;
023
024import org.apache.hadoop.classification.InterfaceAudience.Public;
025import org.apache.hadoop.classification.InterfaceStability.Stable;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
028import org.apache.hadoop.yarn.util.Records;
029
030/**
031 * <p><code>URL</code> represents a serializable {@link java.net.URL}.</p>
032 */
033@Public
034@Stable
035public abstract class URL {
036
037  @Public
038  @Stable
039  public static URL newInstance(String scheme, String host, int port, String file) {
040    URL url = Records.newRecord(URL.class);
041    url.setScheme(scheme);
042    url.setHost(host);
043    url.setPort(port);
044    url.setFile(file);
045    return url;
046  }
047
048  /**
049   * Get the scheme of the URL.
050   * @return scheme of the URL
051   */
052  @Public
053  @Stable
054  public abstract String getScheme();
055  
056  /**
057   * Set the scheme of the URL
058   * @param scheme scheme of the URL
059   */
060  @Public
061  @Stable
062  public abstract void setScheme(String scheme);
063
064  /**
065   * Get the user info of the URL.
066   * @return user info of the URL
067   */
068  @Public
069  @Stable
070  public abstract String getUserInfo();
071  
072  /**
073   * Set the user info of the URL.
074   * @param userInfo user info of the URL
075   */
076  @Public
077  @Stable
078  public abstract void setUserInfo(String userInfo);
079
080  /**
081   * Get the host of the URL.
082   * @return host of the URL
083   */
084  @Public
085  @Stable
086  public abstract String getHost();
087  
088  /**
089   * Set the host of the URL.
090   * @param host host of the URL
091   */
092  @Public
093  @Stable
094  public abstract void setHost(String host);
095
096  /**
097   * Get the port of the URL.
098   * @return port of the URL
099   */
100  @Public
101  @Stable
102  public abstract int getPort();
103  
104  /**
105   * Set the port of the URL
106   * @param port port of the URL
107   */
108  @Public
109  @Stable
110  public abstract void setPort(int port);
111
112  /**
113   * Get the file of the URL.
114   * @return file of the URL
115   */
116  @Public
117  @Stable
118  public abstract String getFile();
119  
120  /**
121   * Set the file of the URL.
122   * @param file file of the URL
123   */
124  @Public
125  @Stable
126  public abstract void setFile(String file);
127  
128  @Public
129  @Stable
130  public Path toPath() throws URISyntaxException {
131    String scheme = getScheme() == null ? "" : getScheme();
132    
133    String authority = "";
134    if (getHost() != null) {
135      authority = getHost();
136      if (getUserInfo() != null) {
137        authority = getUserInfo() + "@" + authority;
138      }
139      if (getPort() > 0) {
140        authority += ":" + getPort();
141      }
142    }
143    
144    return new Path(
145        (new URI(scheme, authority, getFile(), null, null)).normalize());
146  }
147  
148  @Public
149  @Stable
150  public static URL fromURI(URI uri) {
151    URL url =
152        RecordFactoryProvider.getRecordFactory(null).newRecordInstance(
153            URL.class);
154    if (uri.getHost() != null) {
155      url.setHost(uri.getHost());
156    }
157    if (uri.getUserInfo() != null) {
158      url.setUserInfo(uri.getUserInfo());
159    }
160    url.setPort(uri.getPort());
161    url.setScheme(uri.getScheme());
162    url.setFile(uri.getPath());
163    return url;
164  }
165  
166  @Public
167  @Stable
168  public static URL fromPath(Path path) {
169    return fromURI(path.toUri());
170  }
171}