Skip to content

Commit

Permalink
Merge pull request #549 from tejones/teiiddes-2822
Browse files Browse the repository at this point in the history
teiiddes-2822
  • Loading branch information
blafond committed Apr 12, 2016
2 parents 4e1995b + c1cea41 commit 4fb7eff
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
5 changes: 5 additions & 0 deletions plugins/org.teiid.designer.datatools.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ Export-Package: org.teiid.designer.datatools.profiles.ldap.widget,
org.teiid.designer.datatools.ui.actions,
org.teiid.designer.datatools.ui.dialogs,
org.teiid.designer.datatools.ui.jobs
Import-Package: org.apache.commons.httpclient,
org.apache.commons.httpclient.auth;version="3.1.0",
org.apache.commons.httpclient.methods;version="3.1.0",
org.apache.commons.httpclient.params;version="3.1.0",
org.apache.http.auth;version="4.3.6"
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ protected IStatus run( IProgressMonitor monitor ) {
}

public Exception testXmlUrlConnection( IConnectionProfile icp ) {
return WSWizardUtils.testURLConnection(icp, IWSProfileConstants.END_POINT_URI_PROP_ID);
return WSWizardUtils.testRestURLConnection(icp, IWSProfileConstants.END_POINT_URI_PROP_ID);
}

public Throwable getTestConnectionException( IConnection conn ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
*/
package org.teiid.designer.datatools.profiles.ws;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.teiid.core.designer.util.Base64;
import org.teiid.datatools.connectivity.model.Parameter;
import org.teiid.designer.core.util.URLHelper;
import org.teiid.designer.datatools.ui.DatatoolsUiConstants;
import org.teiid.designer.ui.common.ICredentialsCommon;
import org.teiid.designer.ui.common.ICredentialsCommon.SecurityType;


/**
Expand All @@ -28,6 +35,103 @@ public class WSWizardUtils {
private WSWizardUtils() {
}

/**
* Extract an {@link URL} from the given {@link IConnectionProfile} using
* the given property key and test its connectivity.
* <p>
* If the URL requires authentication then the authentication security type,
* username and password will all be extracted from the
* {@link IConnectionProfile}. Thus, the values for the authentication
* should be stored in the connection profile using the keys from
* {@link ICredentialsCommon}.
*
*
* @param connectionProfile
* @param propertyKey
* @return
*/
public static Exception testRestURLConnection(IConnectionProfile connectionProfile,
final String propertyKey) {
Properties connProperties = connectionProfile.getBaseProperties();
// InputStream not provided, check XML file
String xmlFile = connProperties == null ? null : (String) connProperties.get(propertyKey);
String responseType = IWSProfileConstants.XML;
if( connProperties != null ) {
if( connProperties.get(IWSProfileConstants.RESPONSE_TYPE_PROPERTY_KEY) != null) {
responseType = (String)connProperties.get(IWSProfileConstants.RESPONSE_TYPE_PROPERTY_KEY);
}
}

try {
URL url = URLHelper.buildURL(xmlFile);

String userName = null;
String password = null;
userName = connProperties.getProperty(ICredentialsCommon.USERNAME_PROP_ID);
password = connProperties.getProperty(ICredentialsCommon.PASSWORD_PROP_ID);
URI uri = url.toURI();
GetMethod httpget = new GetMethod(uri.toString());
String securityType = null;

boolean secure = (userName != null && !userName.isEmpty());

HttpClient client = new HttpClient();
if (secure){
securityType = (String) connProperties.get(ICredentialsCommon.SECURITY_TYPE_ID);
client.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
new UsernamePasswordCredentials(userName, password));
List<String> authPrefs = new ArrayList<String>(1);
if (securityType.equals(ICredentialsCommon.SecurityType.HTTPBasic.toString())) {
authPrefs.add(AuthPolicy.BASIC);
} else if (securityType.equals(ICredentialsCommon.SecurityType.HTTPDigest.toString())) {
authPrefs.add(AuthPolicy.DIGEST);
}
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
}

if (connProperties.get(IWSProfileConstants.ACCEPT_PROPERTY_KEY) != null) {
httpget.addRequestHeader(IWSProfileConstants.ACCEPT_PROPERTY_KEY,
(String) connProperties.get(IWSProfileConstants.ACCEPT_PROPERTY_KEY));
} else {
if (responseType.equalsIgnoreCase(IWSProfileConstants.JSON)) {
httpget.addRequestHeader(IWSProfileConstants.ACCEPT_PROPERTY_KEY,
IWSProfileConstants.CONTENT_TYPE_JSON_VALUE);
} else {
httpget.addRequestHeader(IWSProfileConstants.ACCEPT_PROPERTY_KEY,
IWSProfileConstants.ACCEPT_DEFAULT_VALUE);
}
}

if (connProperties.get(IWSProfileConstants.CONTENT_TYPE_PROPERTY_KEY) != null) {
httpget.addRequestHeader(IWSProfileConstants.CONTENT_TYPE_PROPERTY_KEY,
(String) connProperties.get(IWSProfileConstants.CONTENT_TYPE_PROPERTY_KEY));
} else {
httpget.addRequestHeader(IWSProfileConstants.CONTENT_TYPE_PROPERTY_KEY,
IWSProfileConstants.CONTENT_TYPE_DEFAULT_VALUE);
}

int code =0;
if (secure) {
client.getParams().setAuthenticationPreemptive(true);
httpget.setDoAuthentication(true);
code = client.executeMethod(httpget);
} else {
client.getParams().setAuthenticationPreemptive(false);
httpget.setDoAuthentication(false);
code = client. executeMethod(httpget);
}

if (code!=200) {
throw new Exception(DatatoolsUiConstants.UTIL.getString("WSWizardUtils.connectionFailureMessage")); //$NON-NLS-1$
}

} catch (Exception ex) {
return ex;
}

return null;
}

/**
* Extract an {@link URL} from the given {@link IConnectionProfile} using
* the given property key and test its connectivity.
Expand Down Expand Up @@ -110,5 +214,4 @@ public static Exception testURLConnection(IConnectionProfile connectionProfile,

return null;
}

}

0 comments on commit 4fb7eff

Please sign in to comment.