View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools.url;
12  
13  import java.net.MalformedURLException;
14  
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  /**
19   * The Class UrlParser.
20   */
21  public class UrlParser {
22  
23    /** The Constant logger. */
24    private static final Logger logger = LoggerFactory.getLogger(UrlParser.class);
25  
26    /** The protocol. */
27    private String protocol;
28  
29    /** The host. */
30    private String host;
31  
32    /** The port. */
33    private int port = -1;
34  
35    /** The path. */
36    private String path;
37  
38    /**
39     * Instantiates a new url parser.
40     *
41     * @param url the url
42     *
43     * @throws MalformedURLException the malformed URL exception
44     */
45    public UrlParser(String url) throws MalformedURLException {
46      if (url == null || url.length() <= 0) {
47        throw new MalformedURLException("Empty URL");
48      }
49      int ppos = url.indexOf("://");
50  
51      // get protocol first
52      if (ppos >= 0) {
53        protocol = url.substring(0, ppos);
54        url = url.substring(ppos + 3);
55      }
56  
57      String hostport;
58  
59      ppos = url.indexOf('/');
60      if (ppos >= 0) {
61        hostport = url.substring(0, ppos);
62        path = url.substring(ppos + 1);
63      } else {
64        hostport = url;
65      }
66  
67      ppos = hostport.indexOf(':');
68      if (ppos >= 0) {
69        host = hostport.substring(0, ppos);
70        String portString = hostport.substring(ppos + 1);
71        try {
72          this.port = Integer.parseInt(portString);
73        } catch (NumberFormatException e) {
74          logger.trace("", e);
75          throw new MalformedURLException("Invalid port " + portString);
76        }
77      } else {
78        host = hostport;
79      }
80    }
81  
82    /**
83     * Gets the protocol.
84     *
85     * @return the protocol
86     */
87    public String getProtocol() {
88      return protocol;
89    }
90  
91    /**
92     * Sets the protocol.
93     *
94     * @param protocol the new protocol
95     */
96    public void setProtocol(String protocol) {
97      this.protocol = protocol;
98    }
99  
100   /**
101    * Gets the host.
102    *
103    * @return the host
104    */
105   public String getHost() {
106     return host;
107   }
108 
109   /**
110    * Sets the host.
111    *
112    * @param host the new host
113    */
114   public void setHost(String host) {
115     this.host = host;
116   }
117 
118   /**
119    * Gets the port.
120    *
121    * @return the port
122    */
123   public int getPort() {
124     return port;
125   }
126 
127   /**
128    * Sets the port.
129    *
130    * @param port the new port
131    */
132   public void setPort(int port) {
133     this.port = port;
134   }
135 
136   /**
137    * Gets the path.
138    *
139    * @return the path
140    */
141   public String getPath() {
142     return path;
143   }
144 
145   /**
146    * Sets the path.
147    *
148    * @param path the new path
149    */
150   public void setPath(String path) {
151     this.path = path;
152   }
153 
154 }