Search in sources :

Example 1 with HostParserException

use of ch.cyberduck.core.exception.HostParserException in project cyberduck by iterate-ch.

the class InterarchyBookmarkCollection method parse.

private void parse(final ProtocolFactory protocols, NSDictionary item) {
    final PlistDeserializer bookmark = new PlistDeserializer(item);
    final List<NSDictionary> children = bookmark.listForKey("Children");
    if (null != children) {
        for (NSDictionary child : children) {
            this.parse(protocols, child);
        }
        return;
    }
    final String url = bookmark.stringForKey("URL");
    if (StringUtils.isBlank(url)) {
        // Possibly a folder
        return;
    }
    final Host host;
    try {
        host = new HostParser(protocols).get(url);
    } catch (HostParserException e) {
        log.warn(e);
        return;
    }
    final String title = bookmark.stringForKey("Title");
    if (StringUtils.isNotBlank(title)) {
        host.setNickname(title);
    }
    this.add(host);
}
Also used : PlistDeserializer(ch.cyberduck.core.serializer.impl.jna.PlistDeserializer) NSDictionary(ch.cyberduck.binding.foundation.NSDictionary) HostParser(ch.cyberduck.core.HostParser) Host(ch.cyberduck.core.Host) HostParserException(ch.cyberduck.core.exception.HostParserException)

Example 2 with HostParserException

use of ch.cyberduck.core.exception.HostParserException in project cyberduck by iterate-ch.

the class FetchBookmarkCollection method parse.

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    NSDictionary serialized = NSDictionary.dictionaryWithContentsOfFile(file.getAbsolute());
    if (null == serialized) {
        throw new LocalAccessDeniedException(String.format("Invalid bookmark file %s", file));
    }
    NSDictionary dict = new PlistDeserializer(serialized).objectForKey("Shortcuts v2");
    if (null == dict) {
        throw new LocalAccessDeniedException(String.format("Invalid bookmark file %s", file));
    }
    dict = new PlistDeserializer(dict).objectForKey("Shortcuts");
    if (null == dict) {
        throw new LocalAccessDeniedException(String.format("Invalid bookmark file %s", file));
    }
    List<NSDictionary> shortcuts = new PlistDeserializer(dict).listForKey("Shortcuts");
    for (NSDictionary shortcut : shortcuts) {
        PlistDeserializer reader = new PlistDeserializer(shortcut);
        NSDictionary remote = reader.objectForKey("Remote Item");
        if (null == remote) {
            continue;
        }
        NSDictionary location = new PlistDeserializer(remote).objectForKey("Location");
        if (null == location) {
            continue;
        }
        String url = new PlistDeserializer(location).stringForKey("URL");
        if (null == url) {
            continue;
        }
        final Host host;
        try {
            host = new HostParser(protocols).get(url);
        } catch (HostParserException e) {
            log.warn(e);
            continue;
        }
        host.setNickname(reader.stringForKey("Name"));
        this.add(host);
    }
}
Also used : PlistDeserializer(ch.cyberduck.core.serializer.impl.jna.PlistDeserializer) NSDictionary(ch.cyberduck.binding.foundation.NSDictionary) HostParser(ch.cyberduck.core.HostParser) Host(ch.cyberduck.core.Host) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException) HostParserException(ch.cyberduck.core.exception.HostParserException)

Example 3 with HostParserException

use of ch.cyberduck.core.exception.HostParserException in project cyberduck by iterate-ch.

the class MainController method handleGetURLEvent_withReplyEvent.

/**
 * Extract the URL from the Apple event and handle it here.
 */
@Action
public void handleGetURLEvent_withReplyEvent(NSAppleEventDescriptor event, NSAppleEventDescriptor reply) {
    log.debug(String.format("Received URL from event %s", event));
    final NSAppleEventDescriptor param = event.paramDescriptorForKeyword(keyAEResult);
    if (null == param) {
        log.error("No URL parameter");
        return;
    }
    final String url = param.stringValue();
    if (StringUtils.isEmpty(url)) {
        log.error("URL parameter is empty");
        return;
    }
    switch(url) {
        case "x-cyberduck-action:update":
            updater.check(false);
            break;
        default:
            if (StringUtils.startsWith(url, OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI)) {
                final String action = StringUtils.removeStart(url, OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI);
                final List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(action), Charset.defaultCharset());
                String state = StringUtils.EMPTY;
                String code = StringUtils.EMPTY;
                for (NameValuePair pair : pairs) {
                    if (StringUtils.equals(pair.getName(), "state")) {
                        state = StringUtils.equals(pair.getName(), "state") ? pair.getValue() : StringUtils.EMPTY;
                    }
                    if (StringUtils.equals(pair.getName(), "code")) {
                        code = StringUtils.equals(pair.getName(), "code") ? pair.getValue() : StringUtils.EMPTY;
                    }
                }
                final OAuth2TokenListenerRegistry oauth = OAuth2TokenListenerRegistry.get();
                oauth.notify(state, code);
            } else if (StringUtils.startsWith(url, CteraProtocol.CTERA_REDIRECT_URI)) {
                final String action = StringUtils.removeStart(url, String.format("%s:", preferences.getProperty("oauth.handler.scheme")));
                final List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(action), Charset.defaultCharset());
                String code = StringUtils.EMPTY;
                for (NameValuePair pair : pairs) {
                    if (StringUtils.equals(pair.getName(), "ActivationCode")) {
                        code = StringUtils.equals(pair.getName(), "ActivationCode") ? pair.getValue() : StringUtils.EMPTY;
                    }
                }
                final OAuth2TokenListenerRegistry oauth = OAuth2TokenListenerRegistry.get();
                oauth.notify(StringUtils.EMPTY, code);
            } else {
                try {
                    final Host h = HostParser.parse(url);
                    h.setCredentials(CredentialsConfiguratorFactory.get(h.getProtocol()).configure(h));
                    if (Path.Type.file == detector.detect(h.getDefaultPath())) {
                        final Path file = new Path(PathNormalizer.normalize(h.getDefaultPath()), EnumSet.of(Path.Type.file));
                        TransferControllerFactory.get().start(new DownloadTransfer(h, file, LocalFactory.get(preferences.getProperty("queue.download.folder"), file.getName())), new TransferOptions());
                    } else {
                        for (BrowserController browser : MainController.getBrowsers()) {
                            if (browser.isMounted()) {
                                if (new HostUrlProvider().get(browser.getSession().getHost()).equals(new HostUrlProvider().get(h))) {
                                    // Handle browser window already connected to the same host. #4215
                                    browser.window().makeKeyAndOrderFront(null);
                                    if (Path.Type.directory == detector.detect(h.getDefaultPath())) {
                                        browser.setWorkdir(new Path(PathNormalizer.normalize(h.getDefaultPath()), EnumSet.of(Path.Type.directory)));
                                    }
                                    return;
                                }
                            }
                        }
                        final BrowserController browser = newDocument(false);
                        browser.mount(h);
                    }
                } catch (HostParserException e) {
                    log.warn(e);
                }
            }
    }
}
Also used : OAuth2TokenListenerRegistry(ch.cyberduck.core.oauth.OAuth2TokenListenerRegistry) NameValuePair(org.apache.http.NameValuePair) NSAppleEventDescriptor(ch.cyberduck.binding.foundation.NSAppleEventDescriptor) NSAttributedString(ch.cyberduck.binding.foundation.NSAttributedString) HostParserException(ch.cyberduck.core.exception.HostParserException) TransferOptions(ch.cyberduck.core.transfer.TransferOptions) DownloadTransfer(ch.cyberduck.core.transfer.DownloadTransfer) ArrayList(java.util.ArrayList) List(java.util.List) AbstractBackgroundAction(ch.cyberduck.core.threading.AbstractBackgroundAction) Action(ch.cyberduck.binding.Action)

Example 4 with HostParserException

use of ch.cyberduck.core.exception.HostParserException in project cyberduck by iterate-ch.

the class DownloadController method callback.

@Override
public void callback(final int returncode) {
    switch(returncode) {
        case DEFAULT_OPTION:
            try {
                final Host host = HostParser.parse(urlField.stringValue());
                final Path file = new Path(PathNormalizer.normalize(host.getDefaultPath()), EnumSet.of(detector.detect(host.getDefaultPath())));
                host.setDefaultPath(file.getParent().getAbsolute());
                final Transfer transfer = new DownloadTransfer(host, file, LocalFactory.get(PreferencesFactory.get().getProperty("queue.download.folder"), file.getName()));
                TransferControllerFactory.get().start(transfer, new TransferOptions());
                break;
            } catch (HostParserException e) {
                log.warn(e);
            }
    }
}
Also used : Path(ch.cyberduck.core.Path) TransferOptions(ch.cyberduck.core.transfer.TransferOptions) DownloadTransfer(ch.cyberduck.core.transfer.DownloadTransfer) Transfer(ch.cyberduck.core.transfer.Transfer) DownloadTransfer(ch.cyberduck.core.transfer.DownloadTransfer) Host(ch.cyberduck.core.Host) HostParserException(ch.cyberduck.core.exception.HostParserException)

Example 5 with HostParserException

use of ch.cyberduck.core.exception.HostParserException in project cyberduck by iterate-ch.

the class HostParser method parsePort.

static void parsePort(final StringReader reader, final Host host, final Consumer<HostParserException> decorator) throws HostParserException {
    Integer port = null;
    int tracker = reader.position;
    while (!reader.endOfString()) {
        final char c = (char) reader.read();
        if (Character.isDigit(c)) {
            port = Optional.ofNullable(port).orElse(0) * 10 + Character.getNumericValue(c);
        } else {
            if (c != '/') {
                port = null;
                log.warn(String.format("Got %s in port. This is unsupported. Continuing with default port", c));
                reader.skip(tracker - reader.position);
            } else {
                reader.skip(-1);
            }
            break;
        }
    }
    if (port != null && host.getProtocol().isPortConfigurable()) {
        if (port <= 0 || port >= 65536) {
            throw decorate(new HostParserException(String.format("Port %d is outside allowed range 0-65536", port)), decorator);
        }
        host.setPort(port);
    }
}
Also used : HostParserException(ch.cyberduck.core.exception.HostParserException)

Aggregations

HostParserException (ch.cyberduck.core.exception.HostParserException)13 Host (ch.cyberduck.core.Host)5 HostParser (ch.cyberduck.core.HostParser)4 ArrayList (java.util.ArrayList)4 NSObject (ch.cyberduck.binding.foundation.NSObject)3 LocalAccessDeniedException (ch.cyberduck.core.exception.LocalAccessDeniedException)3 TransferOptions (ch.cyberduck.core.transfer.TransferOptions)3 List (java.util.List)3 NSPoint (org.rococoa.cocoa.foundation.NSPoint)3 Action (ch.cyberduck.binding.Action)2 NSArray (ch.cyberduck.binding.foundation.NSArray)2 NSAttributedString (ch.cyberduck.binding.foundation.NSAttributedString)2 NSDictionary (ch.cyberduck.binding.foundation.NSDictionary)2 Path (ch.cyberduck.core.Path)2 PlistDeserializer (ch.cyberduck.core.serializer.impl.jna.PlistDeserializer)2 AbstractBackgroundAction (ch.cyberduck.core.threading.AbstractBackgroundAction)2 DownloadTransfer (ch.cyberduck.core.transfer.DownloadTransfer)2 Transfer (ch.cyberduck.core.transfer.Transfer)2 NSInteger (org.rococoa.cocoa.foundation.NSInteger)2 HyperlinkAttributedStringFactory (ch.cyberduck.binding.HyperlinkAttributedStringFactory)1