/**
* Copyright (C) 2009-2015 Dell, Inc
* See annotations for authorship information
*
* ====================================================================
* 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 org.dasein.security.joyent;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.joyent.SmartDataCenter;
import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
public class AuthClientFactory extends DefaultClientFactory implements JoyentClientFactory {
public AuthClientFactory(ProviderContext providerContext) {
super(providerContext);
}
@Override
public @Nonnull HttpClient getClient(String endpoint) throws CloudException, InternalException {
if( endpoint == null ) {
throw new CloudException("No cloud endpoint was defined");
}
boolean ssl = endpoint.startsWith("https");
int targetPort;
URI uri;
try {
uri = new URI(endpoint);
targetPort = uri.getPort();
if( targetPort < 1 ) {
targetPort = (ssl ? 443 : 80);
}
}
catch( URISyntaxException e ) {
throw new CloudException(e);
}
HttpHost targetHost = new HttpHost(uri.getHost(), targetPort, uri.getScheme());
DefaultHttpClient client = (DefaultHttpClient) super.getClient(endpoint);
try {
String userName = new String(getProviderContext().getAccessPublic(), "utf-8");
String password = new String(getProviderContext().getAccessPrivate(), "utf-8");
client.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(userName, password));
}
catch( UnsupportedEncodingException e ) {
throw new InternalException(e);
}
return client;
}
}