-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/blackducksoftware/hub-common
- Loading branch information
Showing
8 changed files
with
443 additions
and
258 deletions.
There are no files selected for viewing
297 changes: 75 additions & 222 deletions
297
src/main/java/com/blackducksoftware/integration/hub/HubIntRestService.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/blackducksoftware/integration/hub/util/AuthenticatorUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2016 Black Duck Software, Inc. | ||
* http://www.blackducksoftware.com/ | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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.blackducksoftware.integration.hub.util; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class AuthenticatorUtil { | ||
|
||
public static void resetAuthenticator() { | ||
Authenticator.setDefault(null); | ||
|
||
attemptResetProxyCache(); | ||
} | ||
|
||
public static void setAuthenticator(final String proxyUser, final String proxyPassword) { | ||
if (!StringUtils.isBlank(proxyUser) && !StringUtils.isBlank(proxyPassword)) { | ||
Authenticator.setDefault(new Authenticator() { | ||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
if (getRequestorType() == RequestorType.PROXY) { | ||
return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray()); | ||
} | ||
return null; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private static void attemptResetProxyCache() { | ||
try { | ||
Class<?> sunAuthCacheValue; | ||
Class<?> sunAuthCache; | ||
Class<?> sunAuthCacheImpl; | ||
try { | ||
sunAuthCacheValue = Class.forName("sun.net.www.protocol.http.AuthCacheValue"); | ||
sunAuthCache = Class.forName("sun.net.www.protocol.http.AuthCache"); | ||
sunAuthCacheImpl = Class.forName("sun.net.www.protocol.http.AuthCacheImpl"); | ||
} catch (final Exception e) { | ||
// Must not be using a JDK with sun classes so we abandon this | ||
// reset since it is sun specific | ||
return; | ||
} | ||
|
||
final java.lang.reflect.Method m = sunAuthCacheValue.getDeclaredMethod("setAuthCache", sunAuthCache); | ||
|
||
final Constructor<?> authCacheImplConstr = sunAuthCacheImpl.getConstructor(); | ||
final Object authCachImp = authCacheImplConstr.newInstance(); | ||
|
||
m.invoke(null, authCachImp); | ||
} catch (final Exception e) { | ||
System.err.println(e.toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.