com.qwazr.cluster.manager.ClusterNodeAddress Maven / Gradle / Ivy
Show all versions of qwazr-cluster Show documentation
/**
* Copyright 2015-2016 Emmanuel Keller / QWAZR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qwazr.cluster.manager;
import com.qwazr.utils.StringUtils;
import com.qwazr.utils.server.ServerException;
import javax.ws.rs.core.Response;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
public class ClusterNodeAddress {
public final URI uri;
public final InetSocketAddress address;
public final String httpAddressKey;
/**
* @param httpAddress the address of the node: {scheme}://{host}:{port}
*/
ClusterNodeAddress(String httpAddress, int defaultPort) {
try {
if (!httpAddress.contains("//"))
httpAddress = "http://" + httpAddress;
final URI u = new URI(httpAddress);
uri = new URI(StringUtils.isEmpty(u.getScheme()) ? "http" : u.getScheme(), null, u.getHost(),
u.getPort() == -1 ? defaultPort : u.getPort(), null, null, null);
address = new InetSocketAddress(uri.getHost(), uri.getPort());
httpAddressKey = uri.toString().intern();
} catch (URISyntaxException e) {
throw new ServerException(Response.Status.NOT_ACCEPTABLE,
"Cannot create node. Wrong address syntax: " + httpAddress, e);
}
}
}