/**
  * @param distribution Configuration
  * @return Status message from service
  * @throws IOException Service error
  */
 private String readInvalidationStatus(Distribution distribution) throws IOException {
   try {
     final CloudFrontService cf = this.getClient();
     boolean complete = false;
     int inprogress = 0;
     List<InvalidationSummary> summaries = cf.listInvalidations(distribution.getId());
     for (InvalidationSummary s : summaries) {
       if ("Completed".equals(s.getStatus())) {
         // No schema for status enumeration. Fail.
         complete = true;
       } else {
         // InProgress
         inprogress++;
       }
     }
     if (inprogress > 0) {
       return MessageFormat.format(
           Locale.localizedString("{0} invalidations in progress", "S3"), inprogress);
     }
     if (complete) {
       return MessageFormat.format(
           Locale.localizedString("{0} invalidations completed", "S3"), summaries.size());
     }
     return Locale.localizedString("None");
   } catch (CloudFrontServiceException e) {
     this.error("Cannot read CDN configuration", e);
   }
   return Locale.localizedString("Unknown");
 }
  /**
   * You can make any number of invalidation requests, but you can have only three invalidation
   * requests in progress at one time. Each request can contain up to 1,000 objects to invalidate.
   * If you exceed these limits, you get an error message.
   *
   * <p>It usually takes 10 to 15 minutes to complete your invalidation request, depending on the
   * size of your request.
   *
   * @param origin Origin server
   * @param method Distribution method
   * @param files Files to purge
   * @param recursive Recursivly for folders
   */
  @Override
  public void invalidate(
      String origin, Distribution.Method method, List<Path> files, boolean recursive) {
    try {
      this.check();
      this.message(
          MessageFormat.format(
              Locale.localizedString("Writing CDN configuration of {0}", "Status"), origin));

      final long reference = System.currentTimeMillis();
      Distribution d = distributionStatus.get(method).get(origin);
      if (null == d) {
        log.error(String.format("No cached distribution for origin %s", origin));
        return;
      }
      List<String> keys = this.getInvalidationKeys(files, recursive);
      if (keys.isEmpty()) {
        log.warn("No keys selected for invalidation");
        return;
      }
      CloudFrontService cf = this.getClient();
      cf.invalidateObjects(
          d.getId(),
          keys.toArray(new String[keys.size()]), // objects
          new Date(reference).toString() // Comment
          );
    } catch (CloudFrontServiceException e) {
      this.error("Cannot write CDN configuration", e);
    } catch (IOException e) {
      this.error("Cannot write CDN configuration", e);
    } finally {
      distributionStatus.get(method).clear();
    }
  }
  @Override
  public Distribution read(String origin, Distribution.Method method) {
    if (method.equals(Distribution.DOWNLOAD)
        || method.equals(Distribution.STREAMING)
        || method.equals(Distribution.CUSTOM)
        || method.equals(Distribution.WEBSITE_CDN)) {
      if (!distributionStatus.get(method).containsKey(origin)) {
        try {
          this.check();
          this.message(
              MessageFormat.format(
                  Locale.localizedString("Reading CDN configuration of {0}", "Status"), origin));

          this.cache(origin, method);
        } catch (CloudFrontServiceException e) {
          this.error("Cannot read CDN configuration", e);
        } catch (LoginCanceledException canceled) {
          // User canceled Cloudfront login. Possibly not enabled in Amazon configuration.
          distributionStatus
              .get(method)
              .put(
                  origin,
                  new Distribution(null, origin, method, false, null, canceled.getMessage()));
        } catch (IOException e) {
          this.error("Cannot read CDN configuration", e);
        }
      }
    }
    if (distributionStatus.get(method).containsKey(origin)) {
      return distributionStatus.get(method).get(origin);
    }
    return new Distribution(origin, method);
  }
  @Override
  protected void login(LoginController controller, Credentials credentials) throws IOException {
    try {
      client =
          new CloudFrontService(
              new AWSCredentials(credentials.getUsername(), credentials.getPassword())) {

            @Override
            protected HttpClient initHttpConnection() {
              return CloudFrontDistributionConfiguration.this.http();
            }
          };
      // Provoke authentication error
      for (String container : this.getContainers()) {
        for (Distribution.Method method : getMethods(container)) {
          // Cache first container
          this.cache(this.getOrigin(method, container), method);
          break;
        }
        break;
      }
    } catch (CloudFrontServiceException e) {
      log.warn(String.format("Invalid account: %s", e.getMessage()));
      this.message(Locale.localizedString("Login failed", "Credentials"));
      controller.fail(host.getProtocol(), credentials);
      this.login();
    }
  }
 private void setStatusText() {
   statusField.setAttributedStringValue(
       NSAttributedString.attributedStringWithAttributes(
           transfer.isRunning()
               ? StringUtils.EMPTY
               : Locale.localizedString(transfer.getStatus(), "Status"),
           TRUNCATE_MIDDLE_ATTRIBUTES));
 }
Beispiel #6
0
 public void mountFailed(NSNotification notification) {
   log.warn("mountFailed");
   NSDictionary userInfo = notification.userInfo();
   NSError error =
       Rococoa.cast(
           userInfo.objectForKey(GMUserFileSystem.kGMUserFileSystemErrorKey), NSError.class);
   session.error(Locale.localizedString("Mount failed", "Error"), null);
 }
 @Action
 public void pkCheckboxSelectionChanged(final NSButton sender) {
   log.debug("pkCheckboxSelectionChanged");
   if (sender.state() == NSCell.NSOnState) {
     publicKeyPanel = NSOpenPanel.openPanel();
     publicKeyPanel.setCanChooseDirectories(false);
     publicKeyPanel.setCanChooseFiles(true);
     publicKeyPanel.setAllowsMultipleSelection(false);
     publicKeyPanel.setMessage(
         Locale.localizedString("Select the private key in PEM or PuTTY format", "Credentials"));
     publicKeyPanel.setPrompt(Locale.localizedString("Choose"));
     publicKeyPanel.beginSheetForDirectory(
         LocalFactory.createLocal("~/.ssh").getAbsolute(),
         null,
         this.window(),
         this.id(),
         Foundation.selector("pkSelectionPanelDidEnd:returnCode:contextInfo:"),
         null);
   } else {
     this.pkSelectionPanelDidEnd_returnCode_contextInfo(
         publicKeyPanel, NSPanel.NSCancelButton, null);
   }
 }
 private Distribution convert(
     final org.jets3t.service.model.cloudfront.Distribution d, Distribution.Method method)
     throws IOException, CloudFrontServiceException {
   // Retrieve distributions configuration to access current logging status settings.
   final DistributionConfig distributionConfig = this.getDistributionConfig(d);
   final String loggingTarget;
   if (null == distributionConfig.getLoggingStatus()) {
     // Default logging target to origin itself
     loggingTarget =
         ServiceUtils.findBucketNameInHostname(
             d.getConfig().getOrigin().getDomainName(), Protocol.S3_SSL.getDefaultHostname());
   } else {
     loggingTarget =
         ServiceUtils.findBucketNameInHostname(
             distributionConfig.getLoggingStatus().getBucket(),
             Protocol.S3_SSL.getDefaultHostname());
   }
   final Distribution distribution =
       new Distribution(
           d.getId(),
           distributionConfig.getEtag(),
           distributionConfig.getCallerReference(),
           d.getConfig().getOrigin().getDomainName(),
           method,
           d.getConfig().isEnabled(),
           d.isDeployed(),
           // CloudFront URL
           String.format("%s://%s%s", method.getScheme(), d.getDomainName(), method.getContext()),
           method.equals(Distribution.DOWNLOAD) || method.equals(Distribution.CUSTOM)
               ? String.format("https://%s%s", d.getDomainName(), method.getContext())
               : null, // No SSL
           null,
           Locale.localizedString(d.getStatus(), "S3"),
           distributionConfig.getCNAMEs(),
           distributionConfig.getLoggingStatus().isEnabled(),
           loggingTarget,
           distributionConfig.getDefaultRootObject());
   if (this.isInvalidationSupported(method)) {
     distribution.setInvalidationStatus(this.readInvalidationStatus(distribution));
   }
   if (this.isLoggingSupported(method)) {
     distribution.setContainers(this.getContainers());
   }
   return distribution;
 }
 private void init() {
   window.setTitle(host.getNickname());
   this.updateField(hostField, host.getHostname());
   hostField.setEnabled(host.getProtocol().isHostnameConfigurable());
   hostField.cell().setPlaceholderString(host.getProtocol().getDefaultHostname());
   this.updateField(nicknameField, host.getNickname());
   final String url;
   if (StringUtils.isNotBlank(host.getDefaultPath())) {
     url = host.toURL() + Path.normalize(host.getDefaultPath());
   } else {
     url = host.toURL();
   }
   urlField.setAttributedStringValue(HyperlinkAttributedStringFactory.create(url));
   this.updateField(portField, String.valueOf(host.getPort()));
   portField.setEnabled(host.getProtocol().isPortConfigurable());
   this.updateField(pathField, host.getDefaultPath());
   this.updateField(usernameField, host.getCredentials().getUsername());
   usernameField.cell().setPlaceholderString(host.getProtocol().getUsernamePlaceholder());
   usernameField.setEnabled(!host.getCredentials().isAnonymousLogin());
   anonymousCheckbox.setEnabled(host.getProtocol().isAnonymousConfigurable());
   anonymousCheckbox.setState(
       host.getCredentials().isAnonymousLogin() ? NSCell.NSOnState : NSCell.NSOffState);
   protocolPopup.selectItemAtIndex(
       protocolPopup.indexOfItemWithRepresentedObject(
           String.valueOf(host.getProtocol().hashCode())));
   if (null == host.getMaxConnections()) {
     transferPopup.selectItemWithTitle(DEFAULT);
   } else {
     transferPopup.selectItemWithTitle(
         host.getMaxConnections() == 1 ? TRANSFER_BROWSERCONNECTION : TRANSFER_NEWCONNECTION);
   }
   encodingPopup.setEnabled(host.getProtocol().isEncodingConfigurable());
   connectmodePopup.setEnabled(host.getProtocol().isConnectModeConfigurable());
   if (host.getProtocol().isConnectModeConfigurable()) {
     if (null == host.getFTPConnectMode()) {
       connectmodePopup.selectItemWithTitle(DEFAULT);
     } else if (host.getFTPConnectMode().equals(FTPConnectMode.PASV)) {
       connectmodePopup.selectItemWithTitle(CONNECTMODE_PASSIVE);
     } else if (host.getFTPConnectMode().equals(FTPConnectMode.PORT)) {
       connectmodePopup.selectItemWithTitle(CONNECTMODE_ACTIVE);
     }
   }
   pkCheckbox.setEnabled(host.getProtocol().equals(Protocol.SFTP));
   if (host.getCredentials().isPublicKeyAuthentication()) {
     pkCheckbox.setState(NSCell.NSOnState);
     this.updateField(pkLabel, host.getCredentials().getIdentity().getAbbreviatedPath());
     pkLabel.setTextColor(NSColor.textColor());
   } else {
     pkCheckbox.setState(NSCell.NSOffState);
     pkLabel.setStringValue(Locale.localizedString("No private key selected"));
     pkLabel.setTextColor(NSColor.disabledControlTextColor());
   }
   webURLField.setEnabled(host.getProtocol().isWebUrlConfigurable());
   final String webURL = host.getWebURL();
   webUrlImage.setToolTip(webURL);
   this.updateField(webURLField, host.getDefaultWebURL().equals(webURL) ? null : webURL);
   this.updateField(commentField, host.getComment());
   this.timezonePopup.setEnabled(!host.getProtocol().isUTCTimezone());
   if (null == host.getTimezone()) {
     if (host.getProtocol().isUTCTimezone()) {
       this.timezonePopup.setTitle(UTC.getID());
     } else {
       if (Preferences.instance().getBoolean("ftp.timezone.auto")) {
         this.timezonePopup.setTitle(AUTO);
       } else {
         this.timezonePopup.setTitle(
             TimeZone.getTimeZone(Preferences.instance().getProperty("ftp.timezone.default"))
                 .getID());
       }
     }
   } else {
     this.timezonePopup.setTitle(host.getTimezone().getID());
   }
 }
/** @version $Id: BookmarkController.java 10350 2012-10-17 19:54:04Z dkocher $ */
public class BookmarkController extends WindowController {
  private static Logger log = Logger.getLogger(BookmarkController.class);

  @Outlet private NSPopUpButton protocolPopup;

  public void setProtocolPopup(NSPopUpButton protocolPopup) {
    this.protocolPopup = protocolPopup;
    this.protocolPopup.setEnabled(true);
    this.protocolPopup.setTarget(this.id());
    this.protocolPopup.setAction(Foundation.selector("protocolSelectionChanged:"));
    this.protocolPopup.removeAllItems();
    for (Protocol protocol : ProtocolFactory.getKnownProtocols()) {
      final String title = protocol.getDescription();
      this.protocolPopup.addItemWithTitle(title);
      final NSMenuItem item = this.protocolPopup.itemWithTitle(title);
      item.setRepresentedObject(String.valueOf(protocol.hashCode()));
      item.setImage(IconCache.iconNamed(protocol.icon(), 16));
    }
  }

  @Action
  public void protocolSelectionChanged(final NSPopUpButton sender) {
    log.debug("protocolSelectionChanged:" + sender);
    final Protocol selected =
        ProtocolFactory.forName(protocolPopup.selectedItem().representedObject());
    host.setPort(selected.getDefaultPort());
    if (!host.getProtocol().isHostnameConfigurable()) {
      // Previously selected protocol had a default hostname. Change to default
      // of newly selected protocol.
      host.setHostname(selected.getDefaultHostname());
    }
    if (!selected.isHostnameConfigurable()) {
      // Hostname of newly selected protocol is not configurable. Change to default.
      host.setHostname(selected.getDefaultHostname());
    }
    if (StringUtils.isNotBlank(selected.getDefaultHostname())) {
      // Prefill with default hostname
      host.setHostname(selected.getDefaultHostname());
    }
    if (!selected.isWebUrlConfigurable()) {
      host.setWebURL(null);
    }
    host.setProtocol(selected);
    this.itemChanged();
    this.init();
    this.reachable();
  }

  @Outlet private NSPopUpButton encodingPopup;

  public void setEncodingPopup(NSPopUpButton encodingPopup) {
    this.encodingPopup = encodingPopup;
    this.encodingPopup.setEnabled(true);
    this.encodingPopup.removeAllItems();
    this.encodingPopup.addItemWithTitle(DEFAULT);
    this.encodingPopup.menu().addItem(NSMenuItem.separatorItem());
    this.encodingPopup.addItemsWithTitles(
        NSArray.arrayWithObjects(MainController.availableCharsets()));
    if (null == host.getEncoding()) {
      this.encodingPopup.selectItemWithTitle(DEFAULT);
    } else {
      this.encodingPopup.selectItemWithTitle(host.getEncoding());
    }
    this.encodingPopup.setTarget(this.id());
    final Selector action = Foundation.selector("encodingSelectionChanged:");
    this.encodingPopup.setAction(action);
  }

  @Action
  public void encodingSelectionChanged(final NSPopUpButton sender) {
    log.debug("encodingSelectionChanged:" + sender);
    if (sender.selectedItem().title().equals(DEFAULT)) {
      host.setEncoding(null);
    } else {
      host.setEncoding(sender.selectedItem().title());
    }
    this.itemChanged();
  }

  @Outlet private NSTextField nicknameField;

  public void setNicknameField(NSTextField nicknameField) {
    this.nicknameField = nicknameField;
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("nicknameInputDidChange:"),
            NSControl.NSControlTextDidChangeNotification,
            this.nicknameField);
  }

  @Outlet private NSTextField hostField;

  public void setHostField(NSTextField hostField) {
    this.hostField = hostField;
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("hostFieldDidChange:"),
            NSControl.NSControlTextDidChangeNotification,
            hostField);
  }

  @Outlet private NSButton alertIcon;

  public void setAlertIcon(NSButton alertIcon) {
    this.alertIcon = alertIcon;
    this.alertIcon.setEnabled(false);
    this.alertIcon.setImage(null);
    this.alertIcon.setTarget(this.id());
    this.alertIcon.setAction(Foundation.selector("launchNetworkAssistant:"));
  }

  @Action
  public void launchNetworkAssistant(final NSButton sender) {
    ReachabilityFactory.get().diagnose(host);
  }

  @Outlet private NSTextField portField;

  public void setPortField(NSTextField portField) {
    this.portField = portField;
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("portInputDidEndEditing:"),
            NSControl.NSControlTextDidChangeNotification,
            this.portField);
  }

  @Outlet private NSTextField pathField;

  public void setPathField(NSTextField pathField) {
    this.pathField = pathField;
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("pathInputDidChange:"),
            NSControl.NSControlTextDidChangeNotification,
            this.pathField);
  }

  @Outlet private NSTextField urlField;

  public void setUrlField(NSTextField urlField) {
    this.urlField = urlField;
    this.urlField.setAllowsEditingTextAttributes(true);
    this.urlField.setSelectable(true);
  }

  @Outlet private NSTextField usernameField;

  public void setUsernameField(NSTextField usernameField) {
    this.usernameField = usernameField;
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("usernameInputDidChange:"),
            NSControl.NSControlTextDidChangeNotification,
            this.usernameField);
  }

  @Outlet private NSButton anonymousCheckbox;

  public void setAnonymousCheckbox(NSButton anonymousCheckbox) {
    this.anonymousCheckbox = anonymousCheckbox;
    this.anonymousCheckbox.setTarget(this.id());
    this.anonymousCheckbox.setAction(Foundation.selector("anonymousCheckboxClicked:"));
    this.anonymousCheckbox.setState(NSCell.NSOffState);
  }

  @Outlet private NSTextField webURLField;

  public void setWebURLField(NSTextField webURLField) {
    this.webURLField = webURLField;
    final NSTextFieldCell cell = this.webURLField.cell();
    cell.setPlaceholderString(host.getDefaultWebURL());
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("webURLInputDidChange:"),
            NSControl.NSControlTextDidChangeNotification,
            this.webURLField);
  }

  @Outlet private NSButton webUrlImage;

  public void setWebUrlImage(NSButton b) {
    this.webUrlImage = b;
    this.webUrlImage.setTarget(this.id());
    this.webUrlImage.setAction(Foundation.selector("openWebUrl:"));
    this.webUrlImage.setImage(IconCache.iconNamed("site.tiff", 16));
  }

  private NSImage favicon;

  /** */
  private void updateFavicon() {
    if (Preferences.instance().getBoolean("bookmark.favicon.download")) {
      this.background(
          new AbstractBackgroundAction<Void>() {

            @Override
            public void run() {
              NSImage img;
              final String f = host.getProtocol().favicon();
              if (StringUtils.isNotBlank(f)) {
                img = IconCache.iconNamed(f, 16);
              } else {
                String url = host.getWebURL() + "/favicon.ico";
                // Default favicon location
                final NSData data = NSData.dataWithContentsOfURL(NSURL.URLWithString(url));
                if (null == data) {
                  return;
                }
                img = NSImage.imageWithData(data);
              }
              if (null == img) {
                return;
              }
              favicon = IconCache.instance().convert(img, 16);
            }

            @Override
            public void cleanup() {
              if (null == favicon) {
                return;
              }
              webUrlImage.setImage(favicon);
            }

            @Override
            public Object lock() {
              return BookmarkController.this;
            }
          });
    }
  }

  @Action
  public void openWebUrl(final NSButton sender) {
    openUrl(host.getWebURL());
  }

  @Outlet private NSTextView commentField;

  public void setCommentField(NSTextView commentField) {
    this.commentField = commentField;
    this.commentField.setFont(NSFont.userFixedPitchFontOfSize(11f));
    NSNotificationCenter.defaultCenter()
        .addObserver(
            this.id(),
            Foundation.selector("commentInputDidChange:"),
            NSText.TextDidChangeNotification,
            this.commentField);
  }

  /** Calculate timezone */
  protected static final String AUTO = Locale.localizedString("Auto");

  @Outlet private NSPopUpButton timezonePopup;

  private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

  private static final String TIMEZONE_CONTINENT_PREFIXES =
      "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";

  public void setTimezonePopup(NSPopUpButton timezonePopup) {
    this.timezonePopup = timezonePopup;
    this.timezonePopup.setTarget(this.id());
    this.timezonePopup.setAction(Foundation.selector("timezonePopupClicked:"));
    this.timezonePopup.removeAllItems();
    final List<String> timezones = Arrays.asList(TimeZone.getAvailableIDs());
    this.timezonePopup.addItemWithTitle(UTC.getID());
    this.timezonePopup.lastItem().setRepresentedObject(UTC.getID());
    this.timezonePopup.menu().addItem(NSMenuItem.separatorItem());
    Collections.sort(
        timezones,
        new Comparator<String>() {
          @Override
          public int compare(String o1, String o2) {
            return TimeZone.getTimeZone(o1).getID().compareTo(TimeZone.getTimeZone(o2).getID());
          }
        });
    for (String tz : timezones) {
      if (tz.matches(TIMEZONE_CONTINENT_PREFIXES)) {
        this.timezonePopup.addItemWithTitle(String.format("%s", tz));
        this.timezonePopup.lastItem().setRepresentedObject(tz);
      }
    }
  }

  @Action
  public void timezonePopupClicked(NSPopUpButton sender) {
    String selected = sender.selectedItem().representedObject();
    if (selected.equals(AUTO)) {
      host.setTimezone(null);
    } else {
      String[] ids = TimeZone.getAvailableIDs();
      for (String id : ids) {
        TimeZone tz;
        if ((tz = TimeZone.getTimeZone(id)).getID().equals(selected)) {
          host.setTimezone(tz);
          break;
        }
      }
    }
    this.itemChanged();
  }

  @Outlet private NSPopUpButton connectmodePopup;

  private static final String CONNECTMODE_ACTIVE = Locale.localizedString("Active");
  private static final String CONNECTMODE_PASSIVE = Locale.localizedString("Passive");

  public void setConnectmodePopup(NSPopUpButton connectmodePopup) {
    this.connectmodePopup = connectmodePopup;
    this.connectmodePopup.setTarget(this.id());
    this.connectmodePopup.setAction(Foundation.selector("connectmodePopupClicked:"));
    this.connectmodePopup.removeAllItems();
    this.connectmodePopup.addItemWithTitle(DEFAULT);
    this.connectmodePopup.menu().addItem(NSMenuItem.separatorItem());
    this.connectmodePopup.addItemWithTitle(CONNECTMODE_ACTIVE);
    this.connectmodePopup.addItemWithTitle(CONNECTMODE_PASSIVE);
  }

  @Action
  public void connectmodePopupClicked(final NSPopUpButton sender) {
    if (sender.selectedItem().title().equals(DEFAULT)) {
      host.setFTPConnectMode(null);
    } else if (sender.selectedItem().title().equals(CONNECTMODE_ACTIVE)) {
      host.setFTPConnectMode(FTPConnectMode.PORT);
    } else if (sender.selectedItem().title().equals(CONNECTMODE_PASSIVE)) {
      host.setFTPConnectMode(FTPConnectMode.PASV);
    }
    this.itemChanged();
  }

  @Outlet private NSPopUpButton transferPopup;

  private static final String TRANSFER_NEWCONNECTION =
      Locale.localizedString("Open new connection");
  private static final String TRANSFER_BROWSERCONNECTION =
      Locale.localizedString("Use browser connection");

  public void setTransferPopup(NSPopUpButton transferPopup) {
    this.transferPopup = transferPopup;
    this.transferPopup.setTarget(this.id());
    this.transferPopup.setAction(Foundation.selector("transferPopupClicked:"));
    this.transferPopup.removeAllItems();
    this.transferPopup.addItemWithTitle(DEFAULT);
    this.transferPopup.menu().addItem(NSMenuItem.separatorItem());
    this.transferPopup.addItemWithTitle(TRANSFER_NEWCONNECTION);
    this.transferPopup.addItemWithTitle(TRANSFER_BROWSERCONNECTION);
  }

  @Action
  public void transferPopupClicked(final NSPopUpButton sender) {
    if (sender.selectedItem().title().equals(DEFAULT)) {
      host.setMaxConnections(null);
    } else if (sender.selectedItem().title().equals(TRANSFER_BROWSERCONNECTION)) {
      host.setMaxConnections(1);
    } else if (sender.selectedItem().title().equals(TRANSFER_NEWCONNECTION)) {
      host.setMaxConnections(-1);
    }
    this.itemChanged();
  }

  @Outlet private NSPopUpButton downloadPathPopup;

  private static final String CHOOSE = Locale.localizedString("Choose") + "…";

  public void setDownloadPathPopup(NSPopUpButton downloadPathPopup) {
    this.downloadPathPopup = downloadPathPopup;
    this.downloadPathPopup.setTarget(this.id());
    final Selector action = Foundation.selector("downloadPathPopupClicked:");
    this.downloadPathPopup.setAction(action);
    this.downloadPathPopup.removeAllItems();

    // Default download folder
    this.addDownloadPath(action, host.getDownloadFolder());
    this.downloadPathPopup.menu().addItem(NSMenuItem.separatorItem());
    this.addDownloadPath(
        action,
        LocalFactory.createLocal(Preferences.instance().getProperty("queue.download.folder")));
    // Shortcut to the Desktop
    this.addDownloadPath(action, LocalFactory.createLocal("~/Desktop"));
    // Shortcut to user home
    this.addDownloadPath(action, LocalFactory.createLocal("~"));
    // Shortcut to user downloads for 10.5
    this.addDownloadPath(action, LocalFactory.createLocal("~/Downloads"));
    // Choose another folder

    // Choose another folder
    this.downloadPathPopup.menu().addItem(NSMenuItem.separatorItem());
    this.downloadPathPopup
        .menu()
        .addItemWithTitle_action_keyEquivalent(CHOOSE, action, StringUtils.EMPTY);
    this.downloadPathPopup.lastItem().setTarget(this.id());
  }

  private void addDownloadPath(Selector action, Local f) {
    if (downloadPathPopup.menu().itemWithTitle(f.getDisplayName()) == null) {
      downloadPathPopup
          .menu()
          .addItemWithTitle_action_keyEquivalent(f.getDisplayName(), action, StringUtils.EMPTY);
      downloadPathPopup.lastItem().setTarget(this.id());
      downloadPathPopup.lastItem().setImage(IconCache.instance().iconForPath(f, 16));
      downloadPathPopup.lastItem().setRepresentedObject(f.getAbsolute());
      if (host.getDownloadFolder().equals(f)) {
        downloadPathPopup.selectItem(downloadPathPopup.lastItem());
      }
    }
  }

  private NSOpenPanel downloadPathPanel;

  @Action
  public void downloadPathPopupClicked(final NSMenuItem sender) {
    if (sender.title().equals(CHOOSE)) {
      downloadPathPanel = NSOpenPanel.openPanel();
      downloadPathPanel.setCanChooseFiles(false);
      downloadPathPanel.setCanChooseDirectories(true);
      downloadPathPanel.setAllowsMultipleSelection(false);
      downloadPathPanel.setCanCreateDirectories(true);
      downloadPathPanel.beginSheetForDirectory(
          null,
          null,
          this.window,
          this.id(),
          Foundation.selector("downloadPathPanelDidEnd:returnCode:contextInfo:"),
          null);
    } else {
      host.setDownloadFolder(LocalFactory.createLocal(sender.representedObject()));
      this.itemChanged();
    }
  }

  public void downloadPathPanelDidEnd_returnCode_contextInfo(
      NSOpenPanel sheet, int returncode, ID contextInfo) {
    if (returncode == SheetCallback.DEFAULT_OPTION) {
      NSArray selected = sheet.filenames();
      if ((selected.lastObject()) != null) {
        host.setDownloadFolder(LocalFactory.createLocal(selected.lastObject().toString()));
      }
    }
    downloadPathPopup
        .itemAtIndex(new NSInteger(0))
        .setTitle(host.getDownloadFolder().getDisplayName());
    downloadPathPopup
        .itemAtIndex(new NSInteger(0))
        .setRepresentedObject(host.getDownloadFolder().getAbsolute());
    downloadPathPopup
        .itemAtIndex(new NSInteger(0))
        .setImage(IconCache.instance().iconForPath(host.getDownloadFolder(), 16));
    downloadPathPopup.selectItemAtIndex(new NSInteger(0));
    downloadPathPanel = null;
    this.itemChanged();
  }

  @Outlet private NSButton toggleOptionsButton;

  public void setToggleOptionsButton(NSButton toggleOptionsButton) {
    this.toggleOptionsButton = toggleOptionsButton;
  }

  /** */
  public static class Factory {
    private static final Map<Host, BookmarkController> open =
        new HashMap<Host, BookmarkController>();

    public static BookmarkController create(final Host host) {
      if (open.containsKey(host)) {
        return open.get(host);
      }
      final BookmarkController c =
          new BookmarkController(host) {
            @Override
            public void windowWillClose(NSNotification notification) {
              super.windowWillClose(notification);
              Factory.open.remove(host);
            }
          };
      open.put(host, c);
      return c;
    }
  }

  /** The bookmark */
  private Host host;

  /** @param host The bookmark to edit */
  private BookmarkController(final Host host) {
    this.host = host;
    // Register for bookmark delete event. Will close this window.
    BookmarkCollection.defaultCollection().addListener(bookmarkCollectionListener);
    this.loadBundle();
  }

  private final AbstractCollectionListener<Host> bookmarkCollectionListener =
      new AbstractCollectionListener<Host>() {
        @Override
        public void collectionItemRemoved(Host item) {
          if (item.equals(host)) {
            final NSWindow window = window();
            if (null != window) {
              window.close();
            }
          }
        }
      };

  @Override
  protected void invalidate() {
    Preferences.instance().setProperty("bookmark.toggle.options", this.toggleOptionsButton.state());
    BookmarkCollection.defaultCollection().removeListener(bookmarkCollectionListener);
    super.invalidate();
  }

  @Override
  protected String getBundleName() {
    return "Bookmark";
  }

  @Override
  public void awakeFromNib() {
    this.cascade();
    this.init();
    this.setState(
        this.toggleOptionsButton, Preferences.instance().getBoolean("bookmark.toggle.options"));
    this.reachable();
    this.updateFavicon();

    super.awakeFromNib();
  }

  @Override
  protected double getMaxWindowHeight() {
    return window.frame().size.height.doubleValue();
  }

  @Override
  protected double getMaxWindowWidth() {
    return 600;
  }

  @Outlet private NSTextField pkLabel;

  public void setPkLabel(NSTextField pkLabel) {
    this.pkLabel = pkLabel;
  }

  @Outlet private NSButton pkCheckbox;

  public void setPkCheckbox(NSButton pkCheckbox) {
    this.pkCheckbox = pkCheckbox;
    this.pkCheckbox.setTarget(this.id());
    this.pkCheckbox.setAction(Foundation.selector("pkCheckboxSelectionChanged:"));
  }

  private NSOpenPanel publicKeyPanel;

  @Action
  public void pkCheckboxSelectionChanged(final NSButton sender) {
    log.debug("pkCheckboxSelectionChanged");
    if (sender.state() == NSCell.NSOnState) {
      publicKeyPanel = NSOpenPanel.openPanel();
      publicKeyPanel.setCanChooseDirectories(false);
      publicKeyPanel.setCanChooseFiles(true);
      publicKeyPanel.setAllowsMultipleSelection(false);
      publicKeyPanel.setMessage(
          Locale.localizedString("Select the private key in PEM or PuTTY format", "Credentials"));
      publicKeyPanel.setPrompt(Locale.localizedString("Choose"));
      publicKeyPanel.beginSheetForDirectory(
          LocalFactory.createLocal("~/.ssh").getAbsolute(),
          null,
          this.window(),
          this.id(),
          Foundation.selector("pkSelectionPanelDidEnd:returnCode:contextInfo:"),
          null);
    } else {
      this.pkSelectionPanelDidEnd_returnCode_contextInfo(
          publicKeyPanel, NSPanel.NSCancelButton, null);
    }
  }

  public void pkSelectionPanelDidEnd_returnCode_contextInfo(
      NSOpenPanel sheet, int returncode, ID contextInfo) {
    log.debug("pkSelectionPanelDidEnd");
    if (returncode == NSPanel.NSOKButton) {
      NSArray selected = sheet.filenames();
      NSEnumerator enumerator = selected.objectEnumerator();
      NSObject next;
      while (((next = enumerator.nextObject()) != null)) {
        host.getCredentials().setIdentity(LocalFactory.createLocal(next.toString()));
      }
    }
    if (returncode == NSPanel.NSCancelButton) {
      host.getCredentials().setIdentity(null);
    }
    this.init();
    this.itemChanged();
  }

  @Action
  public void hostFieldDidChange(final NSNotification sender) {
    String input = hostField.stringValue();
    if (ProtocolFactory.isURL(input)) {
      final Host parsed = Host.parse(input);
      host.setProtocol(parsed.getProtocol());
      host.setPort(parsed.getPort());
      host.setHostname(parsed.getHostname());
      host.setDefaultPath(parsed.getDefaultPath());
    } else {
      host.setHostname(input);
    }
    this.itemChanged();
    this.init();
    this.reachable();
  }

  private void reachable() {
    if (StringUtils.isNotBlank(host.getHostname())) {
      this.background(
          new AbstractBackgroundAction<Void>() {
            boolean reachable = false;

            @Override
            public void run() {
              reachable = ReachabilityFactory.get().isReachable(host);
            }

            @Override
            public void cleanup() {
              alertIcon.setEnabled(!reachable);
              alertIcon.setImage(reachable ? null : IconCache.iconNamed("alert.tiff"));
            }
          });
    } else {
      alertIcon.setImage(IconCache.iconNamed("alert.tiff"));
      alertIcon.setEnabled(false);
    }
  }

  @Action
  public void portInputDidEndEditing(final NSNotification sender) {
    try {
      host.setPort(portField.intValue());
    } catch (NumberFormatException e) {
      host.setPort(-1);
    }
    this.itemChanged();
    this.init();
    this.reachable();
  }

  @Action
  public void pathInputDidChange(final NSNotification sender) {
    host.setDefaultPath(pathField.stringValue());
    this.itemChanged();
    this.init();
  }

  @Action
  public void nicknameInputDidChange(final NSNotification sender) {
    host.setNickname(nicknameField.stringValue());
    this.itemChanged();
    this.init();
  }

  @Action
  public void usernameInputDidChange(final NSNotification sender) {
    host.getCredentials().setUsername(usernameField.stringValue());
    this.itemChanged();
    this.init();
  }

  @Action
  public void anonymousCheckboxClicked(final NSButton sender) {
    if (sender.state() == NSCell.NSOnState) {
      host.getCredentials()
          .setUsername(Preferences.instance().getProperty("connection.login.anon.name"));
    }
    if (sender.state() == NSCell.NSOffState) {
      if (Preferences.instance()
          .getProperty("connection.login.name")
          .equals(Preferences.instance().getProperty("connection.login.anon.name"))) {
        host.getCredentials().setUsername(StringUtils.EMPTY);
      } else {
        host.getCredentials()
            .setUsername(Preferences.instance().getProperty("connection.login.name"));
      }
    }
    this.itemChanged();
    this.init();
  }

  @Action
  public void webURLInputDidChange(final NSNotification sender) {
    host.setWebURL(webURLField.stringValue());
    this.updateFavicon();
    this.itemChanged();
  }

  @Action
  public void commentInputDidChange(final NSNotification sender) {
    host.setComment(commentField.textStorage().string());
    this.itemChanged();
  }

  /**
   * Updates the window title and url label with the properties of this bookmark Propagates all
   * fields with the properties of this bookmark
   */
  private void itemChanged() {
    BookmarkCollection.defaultCollection().collectionItemChanged(host);
  }

  private void init() {
    window.setTitle(host.getNickname());
    this.updateField(hostField, host.getHostname());
    hostField.setEnabled(host.getProtocol().isHostnameConfigurable());
    hostField.cell().setPlaceholderString(host.getProtocol().getDefaultHostname());
    this.updateField(nicknameField, host.getNickname());
    final String url;
    if (StringUtils.isNotBlank(host.getDefaultPath())) {
      url = host.toURL() + Path.normalize(host.getDefaultPath());
    } else {
      url = host.toURL();
    }
    urlField.setAttributedStringValue(HyperlinkAttributedStringFactory.create(url));
    this.updateField(portField, String.valueOf(host.getPort()));
    portField.setEnabled(host.getProtocol().isPortConfigurable());
    this.updateField(pathField, host.getDefaultPath());
    this.updateField(usernameField, host.getCredentials().getUsername());
    usernameField.cell().setPlaceholderString(host.getProtocol().getUsernamePlaceholder());
    usernameField.setEnabled(!host.getCredentials().isAnonymousLogin());
    anonymousCheckbox.setEnabled(host.getProtocol().isAnonymousConfigurable());
    anonymousCheckbox.setState(
        host.getCredentials().isAnonymousLogin() ? NSCell.NSOnState : NSCell.NSOffState);
    protocolPopup.selectItemAtIndex(
        protocolPopup.indexOfItemWithRepresentedObject(
            String.valueOf(host.getProtocol().hashCode())));
    if (null == host.getMaxConnections()) {
      transferPopup.selectItemWithTitle(DEFAULT);
    } else {
      transferPopup.selectItemWithTitle(
          host.getMaxConnections() == 1 ? TRANSFER_BROWSERCONNECTION : TRANSFER_NEWCONNECTION);
    }
    encodingPopup.setEnabled(host.getProtocol().isEncodingConfigurable());
    connectmodePopup.setEnabled(host.getProtocol().isConnectModeConfigurable());
    if (host.getProtocol().isConnectModeConfigurable()) {
      if (null == host.getFTPConnectMode()) {
        connectmodePopup.selectItemWithTitle(DEFAULT);
      } else if (host.getFTPConnectMode().equals(FTPConnectMode.PASV)) {
        connectmodePopup.selectItemWithTitle(CONNECTMODE_PASSIVE);
      } else if (host.getFTPConnectMode().equals(FTPConnectMode.PORT)) {
        connectmodePopup.selectItemWithTitle(CONNECTMODE_ACTIVE);
      }
    }
    pkCheckbox.setEnabled(host.getProtocol().equals(Protocol.SFTP));
    if (host.getCredentials().isPublicKeyAuthentication()) {
      pkCheckbox.setState(NSCell.NSOnState);
      this.updateField(pkLabel, host.getCredentials().getIdentity().getAbbreviatedPath());
      pkLabel.setTextColor(NSColor.textColor());
    } else {
      pkCheckbox.setState(NSCell.NSOffState);
      pkLabel.setStringValue(Locale.localizedString("No private key selected"));
      pkLabel.setTextColor(NSColor.disabledControlTextColor());
    }
    webURLField.setEnabled(host.getProtocol().isWebUrlConfigurable());
    final String webURL = host.getWebURL();
    webUrlImage.setToolTip(webURL);
    this.updateField(webURLField, host.getDefaultWebURL().equals(webURL) ? null : webURL);
    this.updateField(commentField, host.getComment());
    this.timezonePopup.setEnabled(!host.getProtocol().isUTCTimezone());
    if (null == host.getTimezone()) {
      if (host.getProtocol().isUTCTimezone()) {
        this.timezonePopup.setTitle(UTC.getID());
      } else {
        if (Preferences.instance().getBoolean("ftp.timezone.auto")) {
          this.timezonePopup.setTitle(AUTO);
        } else {
          this.timezonePopup.setTitle(
              TimeZone.getTimeZone(Preferences.instance().getProperty("ftp.timezone.default"))
                  .getID());
        }
      }
    } else {
      this.timezonePopup.setTitle(host.getTimezone().getID());
    }
  }
}
Beispiel #11
0
 @Override
 public String getPasswordPlaceholder() {
   return Locale.localizedString("Secret Access Key", "S3");
 }
Beispiel #12
0
 @Override
 public String getUsernamePlaceholder() {
   return Locale.localizedString("Access Key ID", "S3");
 }
  @Override
  public void write(
      boolean enabled,
      String origin,
      Distribution.Method method,
      String[] cnames,
      boolean logging,
      String loggingBucket,
      String defaultRootObject) {
    try {
      this.check();

      // Configure CDN
      LoggingStatus loggingStatus = null;
      if (logging) {
        if (this.isLoggingSupported(method)) {
          final String loggingDestination =
              StringUtils.isNotBlank(loggingBucket)
                  ? ServiceUtils.generateS3HostnameForBucket(
                      loggingBucket, false, Protocol.S3_SSL.getDefaultHostname())
                  : origin;
          loggingStatus =
              new LoggingStatus(
                  loggingDestination,
                  Preferences.instance().getProperty("cloudfront.logging.prefix"));
        }
      }
      StringBuilder name =
          new StringBuilder(Locale.localizedString("Amazon CloudFront", "S3"))
              .append(" ")
              .append(method.toString());
      if (enabled) {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Enable {0} Distribution", "Status"), name));
      } else {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Disable {0} Distribution", "Status"), name));
      }
      Distribution d = distributionStatus.get(method).get(origin);
      if (null == d) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("No existing distribution found for method %s", method));
        }
        this.createDistribution(enabled, method, origin, cnames, loggingStatus, defaultRootObject);
      } else {
        boolean modified = false;
        if (d.isEnabled() != enabled) {
          modified = true;
        }
        if (!Arrays.equals(d.getCNAMEs(), cnames)) {
          modified = true;
        }
        if (d.isLogging() != logging) {
          modified = true;
        }
        // Compare default root object for possible change
        if (!StringUtils.equals(d.getDefaultRootObject(), defaultRootObject)) {
          modified = true;
        }
        // Compare logging target for possible change
        if (!StringUtils.equals(d.getLoggingTarget(), loggingBucket)) {
          modified = true;
        }
        if (modified) {
          this.updateDistribution(
              enabled,
              method,
              origin,
              d.getId(),
              d.getEtag(),
              d.getReference(),
              cnames,
              loggingStatus,
              defaultRootObject);
        } else {
          log.info("Skip updating distribution not modified.");
        }
      }
    } catch (CloudFrontServiceException e) {
      this.error("Cannot write CDN configuration", e);
    } catch (IOException e) {
      this.error("Cannot write CDN configuration", e);
    } finally {
      distributionStatus.get(method).clear();
    }
  }
 @Override
 public String getName() {
   return Locale.localizedString("Amazon CloudFront", "S3");
 }