Select to view content in your preferred language

Using Example from behind a proxy

3560
1
04-25-2014 12:10 AM
LudgerHeck
New Contributor
I am trying to get the example to run from behind a proxy, and somehow I seem to be missing something crucial.
Below is, what I've tried so far
I get


--- result ---
starting main
http://google.com
Response code of the object is 200
OK
http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer
Response code of the object is 200
OK
Java version : 1.7.0_51 (Oracle Corporation) amd64
Rendering engine : DirectX
com.esri.core.io.EsriServiceException: Proxy Authentication Required ( Forefront TMG requires authorization to fulfill the request. Access to the Web
Proxy filter is denied. )
at com.esri.core.internal.io.handler.c.a(Unknown Source)
at com.esri.core.internal.io.handler.n$1.handleResponse(Unknown Source)
at com.esri.core.internal.io.handler.n$1.handleResponse(Unknown Source)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:735)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
at com.esri.core.internal.io.handler.n.a(Unknown Source)
at com.esri.core.internal.io.handler.n.a(Unknown Source)
at com.esri.core.internal.tasks.ags.n.a(Unknown Source)
at com.esri.core.internal.tasks.ags.n.execute(Unknown Source)
at com.esri.map.Layer.loadServiceInfo(Unknown Source)
at com.esri.map.Layer.getMapServerInfo(Unknown Source)
at com.esri.map.ArcGISTiledMapServiceLayer$1.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
--- end of result ---


This seems to me to show that I can pass the proxy in an ordinarily way but somehow not when calling map.getLayers
What am I missing?
Thanks for any help
Ludger


//--- start code ---
import java.awt.EventQueue;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;

import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.ProxySetup;
import com.esri.core.io.UserCredentials;
import com.esri.runtime.ArcGISRuntime;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;

public class MyMapClass {

private JFrame window;
private JMap map;

public MyMapClass() throws IOException, EsriSecurityException {

// Proxy
final String authUser = "me";
final String authPassword = "mypwd";
final String proxyHost = "w3proxy";
final String proxyPort = "8080";
final int iproxyPort = 8080;

window = new JFrame();
window.setSize(800, 600);
window.setLocationRelativeTo(null); // center on screen
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new BorderLayout(0, 0));

System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
System.setProperty("http.proxyType", "4");

Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});

// checks: both ok
URL url = new URL("http://google.com");
HttpURLConnection connection;
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();
System.out.println("http://google.com");
System.out.println("Response code of the object is "+code);
if (code==200)
{
System.out.println("OK");
}

url = new URL("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

code = connection.getResponseCode();
System.out.println("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
System.out.println("Response code of the object is "+code);
if (code==200)
{
System.out.println("OK");
}

// ProxySetup proxySetup = new ProxySetup(); taken from example, what for? Makes no difference
UserCredentials proxyCredentials = new UserCredentials();
proxyCredentials.setUserAccount(authUser, authPassword);

try {
ProxySetup.setupProxy(proxyHost, iproxyPort, "http", proxyCredentials);
}

catch (EsriSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*
UserCredentials uc = new UserCredentials();
uc.setUserAccount(authUser, authPassword);
ProxySetup.setupProxy(proxyHost,8080,uc);
*/

// dispose map just before application window is closed.
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
super.windowClosing(windowEvent);
map.dispose();
}
});

map = new JMap();
window.getContentPane().add(map);

ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" );
//"http://pc20784:6080/arcgis/rest/services/test/strbg/MapServer"); // will do
map.getLayers().add(tiledLayer);
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("starting main");
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
try {
MyMapClass application = new MyMapClass();
application.window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
0 Kudos
1 Reply
MarkBaird
Esri Regular Contributor
You may need to work with your network admin to work out why this is failing.  It should be a case of using the following code:

ProxySetup proxySetup = new ProxySetup();
String proxyHost = "myproxy.domain.com";
int proxyPort = 8080 ;
UserCredentials proxyCredentials = new UserCredentials();
proxyCredentials.setUserAccount("userName", "password");

try {
ProxySetup.setupProxy(proxyHost, proxyPort, proxyCredentials);
}
catch (EsriSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

This doesn't look too far from what you are already trying though.
0 Kudos