Skip to content

Commit

Permalink
first spike at an attempt for #291 - a simple proxy servlet which pas…
Browse files Browse the repository at this point in the history
…ses through the requests; then allowing ?_user=foo&_pwd=bar to be specified on the request URL so that jolokia uses those for its user/pwd (so that the user/pwd isn't part of the URI path). It seems to be able to connect and get the JMX tree; though many operations fail with some JSON issue - but its a start
  • Loading branch information
jstrachan committed May 3, 2013
1 parent 3c100e8 commit 20da6b9
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 3 deletions.
10 changes: 10 additions & 0 deletions hawtio-default/src/main/webapp/WEB-INF/web.xml
Expand Up @@ -27,6 +27,16 @@
<url-pattern>/jolokia/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>jolokia-proxy</servlet-name>
<servlet-class>io.hawt.web.ProxyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jolokia-proxy</servlet-name>
<url-pattern>/proxy/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>maven-source</servlet-name>
<servlet-class>io.hawt.web.JavaDocServlet</servlet-class>
Expand Down
14 changes: 14 additions & 0 deletions hawtio-web/pom.xml
Expand Up @@ -29,6 +29,7 @@
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
Expand All @@ -42,6 +43,19 @@
<version>${project.version}</version>
</dependency>

<!-- for proxy support -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>io.hawt</groupId>
Expand Down
158 changes: 158 additions & 0 deletions hawtio-web/src/main/java/io/hawt/web/ProxyDetails.java
@@ -0,0 +1,158 @@
/**
*
* 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 io.hawt.web;

import io.hawt.util.Strings;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
* A helper object to store the proxy location details
*/
public class ProxyDetails {
private static final transient Logger LOG = LoggerFactory.getLogger(ProxyDetails.class);

private String stringProxyURL;
private String hostAndPort;
private String path = "";
private String userName;
private String password;
private String host;
private int port = 80;

public ProxyDetails(HttpServletRequest httpServletRequest) {
this(httpServletRequest.getPathInfo());

// use request params for the user/pwd
String userParam = httpServletRequest.getParameter("_user");
if (Strings.isNotBlank(userParam)) {
userName = userParam;
}
String pwdParam = httpServletRequest.getParameter("pwd");
if (Strings.isNotBlank(pwdParam)) {
password = pwdParam;
}
}

public ProxyDetails(String pathInfo) {
System.out.println("pathInfo: " + pathInfo);

hostAndPort = pathInfo;
while (hostAndPort.startsWith("/")) {
hostAndPort = hostAndPort.substring(1);
}

// remove user/pwd
int idx = hostAndPort.indexOf("@");
if (idx > 0) {
userName = hostAndPort.substring(0, idx);
hostAndPort = hostAndPort.substring(idx + 1);

idx = userName.indexOf(":");
if (idx > 0) {
password = userName.substring(idx + 1);
userName = userName.substring(0, idx);
}
}
stringProxyURL = "http://" + hostAndPort;

idx = hostAndPort.indexOf("/");
if (idx > 0) {
path = hostAndPort.substring(idx);
hostAndPort = hostAndPort.substring(0, idx);
}

host = hostAndPort;
idx = hostAndPort.indexOf(":");
if (idx > 0) {
host = hostAndPort.substring(0, idx);
String portText = hostAndPort.substring(idx + 1);
port = Integer.parseInt(portText);
}

try {
// Handle the query string
/*
if (httpServletRequest.getQueryString() != null) {
stringProxyURL += "?" + URIUtil.encodeQuery(httpServletRequest.getQueryString());
}
*/
System.out.println("Proxying to " + stringProxyURL);
LOG.debug("Proxying to " + stringProxyURL);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

public HttpClient createHttpClient(HttpMethod httpMethodProxyRequest) {
HttpClient client = new HttpClient();

if (userName != null) {
client.getParams().setAuthenticationPreemptive(true);
httpMethodProxyRequest.setDoAuthentication(true);

Credentials defaultcreds = new UsernamePasswordCredentials(userName, password);
client.getState().setProxyCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
//client.getState().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
}
return client;
}

public String getStringProxyURL() {
return stringProxyURL;
}

public String getProxyHostAndPort() {
return hostAndPort;
}

public String getProxyPath() {
return path;
}

public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getUserName() {
return userName;
}

public String getPassword() {
return password;
}

public String getHostAndPort() {
return hostAndPort;
}

public String getPath() {
return path;
}
}

0 comments on commit 20da6b9

Please sign in to comment.