Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-26580] Updates based on feedback.
  • Loading branch information
akshayabd committed Jan 28, 2015
1 parent 9a427e0 commit 8209680
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 48 deletions.
38 changes: 20 additions & 18 deletions src/main/java/hudson/remoting/Engine.java
Expand Up @@ -24,8 +24,8 @@
package hudson.remoting;

import hudson.remoting.Channel.Mode;
import hudson.remoting.engine.JnlpProtocol;
import hudson.remoting.engine.JnlpProtocolFactory;
import org.jenkinsci.remoting.engine.JnlpProtocol;
import org.jenkinsci.remoting.engine.JnlpProtocolFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -179,18 +179,16 @@ public void run() {

// find out the TCP port
HttpURLConnection con = (HttpURLConnection)salURL.openConnection();
if (con != null) {
if (credentials != null) {
// TODO /tcpSlaveAgentListener is unprotected so why do we need to pass any credentials?
String encoding = Base64.encode(credentials.getBytes("UTF-8"));
con.setRequestProperty("Authorization", "Basic " + encoding);
}

if (proxyCredentials != null) {
String encoding = Base64.encode(proxyCredentials.getBytes("UTF-8"));
con.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
}
}
if (credentials != null) {
// TODO /tcpSlaveAgentListener is unprotected so why do we need to pass any credentials?
String encoding = Base64.encode(credentials.getBytes("UTF-8"));
con.setRequestProperty("Authorization", "Basic " + encoding);
}

if (proxyCredentials != null) {
String encoding = Base64.encode(proxyCredentials.getBytes("UTF-8"));
con.setRequestProperty("Proxy-Authorization", "Basic " + encoding);
}
try {
try {
con.setConnectTimeout(30000);
Expand Down Expand Up @@ -233,29 +231,29 @@ public void run() {
Socket jnlpSocket = connect(port);
DataOutputStream outputStream = new DataOutputStream(jnlpSocket.getOutputStream());
BufferedInputStream inputStream = new BufferedInputStream(jnlpSocket.getInputStream());
String response = "";
boolean connected = false;

// Try available protocols.
for (JnlpProtocol protocol : protocols) {
events.status("Trying protocol: " + protocol.getName());
response = protocol.performHandshake(outputStream, inputStream);
String response = protocol.performHandshake(outputStream, inputStream);

// On success do not try other protocols.
if (response.equals(GREETING_SUCCESS)) {
connected = true;
break;
}

// On failure log the response and form a new connection.
events.status("Server didn't understand the protocol: " + response);
jnlpSocket.close();
Thread.sleep(500);
jnlpSocket = connect(port);
outputStream = new DataOutputStream(jnlpSocket.getOutputStream());
inputStream = new BufferedInputStream(jnlpSocket.getInputStream());
}

// If no protocol worked.
if (!response.equals(GREETING_SUCCESS)) {
if (!connected) {
onConnectionRejected("None of the protocols were accepted");
continue;
}
Expand Down Expand Up @@ -374,5 +372,9 @@ public static Engine current() {

private static final Logger LOGGER = Logger.getLogger(Engine.class.getName());

/**
* @deprecated Use {@link JnlpProtocol#GREETING_SUCCESS}.
*/
@Deprecated
public static final String GREETING_SUCCESS = JnlpProtocol.GREETING_SUCCESS;
}
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;
package org.jenkinsci.remoting.engine;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -43,7 +43,7 @@ public class EngineUtil {
* @return The line read.
* @throws IOException
*/
public static String readLine(InputStream inputStream) throws IOException {
protected static String readLine(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while (true) {
int ch = inputStream.read();
Expand All @@ -62,7 +62,7 @@ public static String readLine(InputStream inputStream) throws IOException {
* @return The set of headers stored in a {@link Properties}.
* @throws IOException
*/
public static Properties readResponseHeaders(BufferedInputStream inputStream) throws IOException {
protected static Properties readResponseHeaders(BufferedInputStream inputStream) throws IOException {
Properties response = new Properties();
while (true) {
String line = EngineUtil.readLine(inputStream);
Expand Down
Expand Up @@ -21,24 +21,24 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;
package org.jenkinsci.remoting.engine;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
* Handshake protocol used by JNLP slave when initiating a connection to
* master.
* master. Protocols may be stateful.
*
* @author Akshay Dayal
*/
public abstract class JnlpProtocol {

final String secretKey;
final String slaveName;
protected final String secretKey;
protected final String slaveName;

JnlpProtocol(String secretKey, String slaveName) {
protected JnlpProtocol(String secretKey, String slaveName) {
this.secretKey = secretKey;
this.slaveName = slaveName;
}
Expand Down
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;
package org.jenkinsci.remoting.engine;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
Expand Down
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;
package org.jenkinsci.remoting.engine;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
Expand Down
Expand Up @@ -21,14 +21,17 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;
package org.jenkinsci.remoting.engine;

import java.util.ArrayList;
import java.util.List;

/**
* Creates protocols to be used to initiate connection with master.
*
* The slave engine will call this factory once when it starts and try the
* protocols in the order they are returned.
*
* @author Akshay Dayal
*/
public class JnlpProtocolFactory {
Expand Down
Expand Up @@ -21,17 +21,17 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;

import static org.junit.Assert.assertEquals;
package org.jenkinsci.remoting.engine;

import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

/**
* Tests for {@link EngineUtil}.
*
Expand Down Expand Up @@ -71,6 +71,6 @@ public void testReadResponseHeadersMultipleFound() throws Exception {
}

private BufferedInputStream stringToStream(String str) {
return new BufferedInputStream(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)));
return new BufferedInputStream(new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8"))));
}
}
Expand Up @@ -21,12 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
package org.jenkinsci.remoting.engine;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -38,6 +33,11 @@
import java.io.BufferedInputStream;
import java.io.DataOutputStream;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* Tests for {@link JnlpProtocol1}.
*
Expand Down
Expand Up @@ -21,14 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.remoting.engine;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;
package org.jenkinsci.remoting.engine;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -44,6 +37,13 @@
import java.util.Date;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

/**
* Tests for {@link JnlpProtocol2}.
*
Expand Down

0 comments on commit 8209680

Please sign in to comment.