Secure WebMap

Download Sample Viewer

Description

This sample shows how to display a secure WebMap housed via ArcGIS Online (http://www.arcgis.com) in a Java application. The user needs to enter a valid combination of ArcGIS Online WebMap item ID, username, and password in the application's input fields to display the corresponding secure map. Anyone can create an account on ArcGIS Online and then proceed to create secure WebMaps.

Code snippet


    try {
        String newId = idMap.get(mapIds.getSelectedItem());
        WebMap webMap = WebMap.newInstance(newId, arcgisPortal);
        map.loadWebMap(webMap);
    } catch (Exception e) {
        // handle exception
    }
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.webmap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.esri.core.io.UserCredentials;
import com.esri.core.portal.Portal;
import com.esri.core.portal.WebMap;
import com.esri.map.JMap;

/**
 * This application shows how to display a secured web map housed on an ArcGIS portal 
 * in a Java application. To use the sample, enter a valid combination of:
 * <ul>
 * <li>ArcGIS portal URL (e.g. 'http://www.arcgis.com' for ArcGIS Online)
 * <li>item ID of a web map
 * <li>username
 * <li>password
 * <li>optionally, enter a Bing Maps key if the web map is using a Bing Maps layer
 * </ul>
 * 
 * Press the button to display the secured map corresponding to the details entered. 
 * Leave the username and password fields blank to display a non-secured web map 
 * (public web map).
 */
public class WebMapSecureApp {

  private JMap map;
  private JTextField portalField;
  private JTextField idField;
  private JTextField usernameField;
  private JPasswordField passwordField;
  private JPasswordField bingKeyField;
  private JPanel inputPanel;
  private JLayeredPane contentPane;

  private static final String DEFAULT_PORTAL_URL = "https://www.arcgis.com";
  private static final Color BG_COLOR = new Color(0, 0, 0, 120);
  private static final Color TRANS_COLOR = new Color(0, 0, 0, 0);

  // default constructor
  public WebMapSecureApp() { }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Loads a secured WebMap using the fields entered by the user
   */
  private void displaySecuredWebMap() {
    try {
      String newId = idField.getText();
      String username = usernameField.getText();
      char[] password = passwordField.getPassword();
      char[] bingKey = bingKeyField.getPassword();
      UserCredentials uc = null;
      if (username != null && !username.isEmpty() && password != null && password.length > 0) {
        uc = new UserCredentials();
        uc.setUserAccount(username, new String(password));
      }
      Portal portal = new Portal(portalField.getText(), uc);
      WebMap webMap = WebMap.newInstance(newId, portal);
      if (bingKey != null && bingKey.length > 0) {
        map.loadWebMap(webMap, new String(bingKey));
      } else {
        map.loadWebMap(webMap);
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          contentPane.getParent(), 
          wrap("Unable to load the web map: " + e.getLocalizedMessage()), 
          "", JOptionPane.ERROR_MESSAGE);
      e.printStackTrace();
    }
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          WebMapSecureApp webMapApp = new WebMapSecureApp();

          JFrame appWindow = webMapApp.createWindow();
          appWindow.add(webMapApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() {

    contentPane = createContentPane();

    // create a blank JMap we will load web maps into
    map = new JMap();

    inputPanel = createPanel();
    inputPanel.setVisible(true);
    inputPanel.setLocation(10, 10);
    contentPane.add(inputPanel);

    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates the application window. Disposes the map on application exit.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame();
    window.setSize(1000, 700);
    window.setLocationRelativeTo(null); // center on screen
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        if (map != null) {
          map.dispose();
        }
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a panel to obtain the portal and web map info from the user.
   * @return a JPanel with text fields and a button
   */
  private JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setSize(380, 180);

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBackground(BG_COLOR);

    JPanel topRow = new JPanel();
    topRow.setBackground(TRANS_COLOR);
    JLabel label = new JLabel("Enter web map and login details below:");
    label.setForeground(Color.white);
    topRow.add(label);

    JPanel buttonRow = new JPanel();
    buttonRow.add(createButton());
    buttonRow.setBackground(TRANS_COLOR);

    panel.add(topRow);
    panel.add(createInnerPanel());
    panel.add(buttonRow);
    panel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));

    return panel;
  }

  private JPanel createInnerPanel() {

    // Create text fields for user input
    portalField = new JTextField(DEFAULT_PORTAL_URL);
    idField = new JTextField(50);
    usernameField = new JTextField(50);
    passwordField = new JPasswordField(50);
    bingKeyField = new JPasswordField();

    // Create labels for the fields
    JLabel portalLabel = new JLabel("Portal URL: ");
    portalLabel.setForeground(Color.white);
    portalLabel.setLabelFor(portalField);
    JLabel idLabel = new JLabel("Map item ID: ");
    idLabel.setForeground(Color.white);
    idLabel.setLabelFor(idField);
    JLabel unLabel = new JLabel("Username: ");
    unLabel.setForeground(Color.white);
    unLabel.setLabelFor(usernameField);
    JLabel pwLabel = new JLabel("Password: ");
    pwLabel.setForeground(Color.white);
    pwLabel.setLabelFor(passwordField);
    JLabel bingKeyLabel = new JLabel("Bing Key: ");
    bingKeyLabel.setForeground(Color.white);
    bingKeyLabel.setLabelFor(bingKeyField);

    JPanel panel = new JPanel();
    panel.setBackground(TRANS_COLOR);
    GridBagLayout gridbag = new GridBagLayout();

    panel.setLayout(gridbag);

    GridBagConstraints c1 = new GridBagConstraints();
    c1.anchor = GridBagConstraints.EAST;
    c1.gridwidth = GridBagConstraints.RELATIVE;
    c1.fill = GridBagConstraints.NONE;
    c1.weightx = 0.0;

    GridBagConstraints c2 = new GridBagConstraints();
    c2.gridwidth = GridBagConstraints.REMAINDER;
    c2.fill = GridBagConstraints.HORIZONTAL;
    c2.weightx = 1.0;

    // add labels and text fields to panel
    panel.add(portalLabel, c1);
    panel.add(portalField, c2);
    panel.add(idLabel, c1);
    panel.add(idField, c2);
    panel.add(unLabel, c1);
    panel.add(usernameField, c2);
    panel.add(pwLabel, c1);
    panel.add(passwordField, c2);
    panel.add(bingKeyLabel, c1);
    panel.add(bingKeyField, c2);

    GridBagConstraints c3 = new GridBagConstraints();
    c3.gridwidth = GridBagConstraints.REMAINDER;
    c3.anchor = GridBagConstraints.WEST;
    c3.weightx = 1.0;

    return panel;
  }

  private JButton createButton() {
    JButton button = new JButton("Display Secured Map");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        displaySecuredWebMap();
      }
    });
    return button;
  }
  
  private String wrap(String str) {
    // create a HTML string that wraps text when longer
    return "<html><p style='width:200px;'>" + str + "</html>";
  }
}
Feedback on this topic?