Search in sources :

Example 56 with MalformedURLException

use of java.net.MalformedURLException in project jodd by oblac.

the class DefaultAppCore method resolveAppDir.

/**
	 * Resolves application root folders.
	 * <p>
	 * If application is started as web application, app folder is one below the WEB-INF folder.
	 * Otherwise, the root folder is equal to the working folder.
	 */
protected void resolveAppDir(String classPathFileName) {
    URL url = ClassLoaderUtil.getResourceUrl(classPathFileName);
    if (url == null) {
        throw new AppException("Failed to resolve app dir, missing: " + classPathFileName);
    }
    String protocol = url.getProtocol();
    if (!protocol.equals("file")) {
        try {
            url = new URL(url.getFile());
        } catch (MalformedURLException ignore) {
        }
    }
    appDir = url.getFile();
    int ndx = appDir.indexOf("WEB-INF");
    isWebApplication = (ndx != -1);
    appDir = isWebApplication ? appDir.substring(0, ndx) : SystemUtil.workingFolder();
}
Also used : AppException(jodd.joy.exception.AppException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL)

Example 57 with MalformedURLException

use of java.net.MalformedURLException in project openhab1-addons by openhab.

the class EBusBinding method updated.

/*
     * (non-Javadoc)
     *
     * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
     */
@Override
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    logger.info("Update eBus Binding configuration ...");
    if (properties == null || properties.isEmpty()) {
        throw new RuntimeException("No properties in openhab.cfg set!");
    }
    try {
        // stop last connector-thread if active
        stopConnector();
        // check to ensure that it is available
        checkConfigurationProvider();
        // clear current configuration
        configurationProvider.clear();
        // load parser from default url
        parser = new EBusTelegramParser(configurationProvider);
        URL configurationUrl = null;
        String parsers = (String) properties.get("parsers");
        if (StringUtils.isEmpty(parsers)) {
            // set to current stable configurations as default
            parsers = "common";
        }
        for (String elem : parsers.split(",")) {
            configurationUrl = null;
            // check for keyword custom to load custom configuration
            if (elem.trim().equals("custom")) {
                String parserUrl = (String) properties.get("parserUrl");
                if (parserUrl != null) {
                    logger.debug("Load custom eBus Parser with url {}", parserUrl);
                    configurationUrl = new URL(parserUrl);
                }
            } else {
                logger.debug("Load eBus Parser Configuration \"{}\" ...", elem.trim());
                String filename = "src/main/resources/" + elem.trim() + "-configuration.json";
                Bundle bundle = FrameworkUtil.getBundle(EBusBinding.class);
                configurationUrl = bundle.getResource(filename);
                if (configurationUrl == null) {
                    logger.error("Unable to load file {} ...", elem.trim() + "-configuration.json");
                }
            }
            if (configurationUrl != null) {
                configurationProvider.loadConfigurationFile(configurationUrl);
            }
        }
        // check minimal config
        if (properties.get("serialPort") != null && properties.get("hostname") != null) {
            throw new ConfigurationException("hostname", "Set property serialPort or hostname, not both!");
        }
        // use the serial connector
        if (StringUtils.isNotEmpty((String) properties.get("serialPort"))) {
            try {
                // load class by reflection to keep gnu.io (serial) optional. Declarative Services causes an
                // class not found exception, also if serial is not used!
                // FIXME: Is there a better way to avoid that a class not found exception?
                @SuppressWarnings("unchecked") Class<AbstractEBusWriteConnector> _tempClass = (Class<AbstractEBusWriteConnector>) EBusBinding.class.getClassLoader().loadClass("org.openhab.binding.ebus.internal.connection.EBusSerialConnector");
                Constructor<AbstractEBusWriteConnector> constructor = _tempClass.getDeclaredConstructor(String.class);
                connector = constructor.newInstance((String) properties.get("serialPort"));
            } catch (ClassNotFoundException e) {
                logger.error(e.toString(), e);
            } catch (NoSuchMethodException e) {
                logger.error(e.toString(), e);
            } catch (SecurityException e) {
                logger.error(e.toString(), e);
            } catch (InstantiationException e) {
                logger.error(e.toString(), e);
            } catch (IllegalAccessException e) {
                logger.error(e.toString(), e);
            } catch (IllegalArgumentException e) {
                logger.error(e.toString(), e);
            } catch (InvocationTargetException e) {
                logger.error(e.toString(), e);
            }
        } else if (StringUtils.isNotEmpty((String) properties.get("hostname"))) {
            // use the tcp-ip connector
            connector = new EBusTCPConnector((String) properties.get("hostname"), Integer.parseInt((String) properties.get("port")));
        }
        // Set eBus sender id or default 0xFF
        if (StringUtils.isNotEmpty((String) properties.get("senderId"))) {
            connector.setSenderId(EBusUtils.toByte((String) properties.get("senderId")));
        }
        if (properties.get("record") != null) {
            String debugWriterMode = (String) properties.get("record");
            logger.info("Enable CSV writer for eBUS {}", debugWriterMode);
            debugWriter = new EBusTelegramCSVWriter();
            debugWriter.openInUserData("ebus-" + debugWriterMode + ".csv");
            parser.setDebugCSVWriter(debugWriter, debugWriterMode);
        }
        // add event listener
        connector.addEBusEventListener(this);
        // start thread
        connector.start();
        // set the new connector
        commandProcessor.setConnector(connector);
        commandProcessor.setConfigurationProvider(configurationProvider);
    } catch (MalformedURLException e) {
        logger.error(e.toString(), e);
    } catch (IOException e) {
        throw new ConfigurationException("general", e.toString(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) EBusTelegramParser(org.openhab.binding.ebus.internal.parser.EBusTelegramParser) AbstractEBusWriteConnector(org.openhab.binding.ebus.internal.connection.AbstractEBusWriteConnector) ConfigurationException(org.osgi.service.cm.ConfigurationException) EBusTCPConnector(org.openhab.binding.ebus.internal.connection.EBusTCPConnector) EBusTelegramCSVWriter(org.openhab.binding.ebus.internal.parser.EBusTelegramCSVWriter)

Example 58 with MalformedURLException

use of java.net.MalformedURLException in project openhab1-addons by openhab.

the class EcoTouchConnector method login.

private void login() {
    cookies = null;
    String url = null;
    try {
        url = "http://" + ip + "/cgi/login?username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");
        URL loginurl = new URL(url);
        URLConnection connection = loginurl.openConnection();
        cookies = connection.getHeaderFields().get("Set-Cookie");
    } catch (MalformedURLException e) {
        logger.debug("The URL '" + url + "' is malformed: " + e.toString());
    } catch (Exception e) {
        logger.debug("Cannot log into Waterkotte EcoTouch: " + e.toString());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) URLConnection(java.net.URLConnection) MalformedURLException(java.net.MalformedURLException)

Example 59 with MalformedURLException

use of java.net.MalformedURLException in project Xponents by OpenSextant.

the class WebLinkTest method test.

/**
     * Tests to see how URLs are mapped to files on disk.  That is, without special chars
     * and choosing best MIME type and file ext.
     */
@Test
public void test() {
    try {
        HyperLink hl = null;
        URL someSite = new URL("http://abc.com/");
        URL pageOnSite = new URL("http://abc.com/abc/xyz.htm");
        hl = new HyperLink("http://abc.com/page.aspx?download=MyHappyFamily.pdf", pageOnSite, someSite);
        print("Found=" + hl.getNormalPath() + " In " + hl.getDirectory());
        hl = new HyperLink("http://abc.com/page.aspx?download=MyHappyFamily.pdf&item=Blue", pageOnSite, someSite);
        print("Found=" + hl.getNormalPath() + " In " + hl.getDirectory());
        hl = new HyperLink("http://abc.com/page.aspx?download=MyHappyFamily.xxz", pageOnSite, someSite);
        print("Found=" + hl.getNormalPath() + " In " + hl.getDirectory());
        hl = new HyperLink("../", pageOnSite, someSite);
        assert (!hl.isCurrentPage());
        hl = new HyperLink("./", pageOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink(".", pageOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink("../", pageOnSite, someSite);
        assert (!hl.isCurrentPage());
        hl = new HyperLink("./a", pageOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink("./a.htm", pageOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink(".", pageOnSite, someSite);
        assert (hl.isCurrentPage());
        // Trailing slash:
        URL folderOnSite = new URL("http://abc.com/abc/");
        hl = new HyperLink("../", folderOnSite, someSite);
        assert (!hl.isCurrentPage());
        try {
            hl = new HyperLink("../", new URL("http://abc.com/abc"), someSite);
            assert (!hl.isCurrentPage());
        } catch (MalformedURLException err) {
            err("Still figuring out what this means. is 'abc' a page or a directory? " + "or does it matter.  Is '..' in relation to that resource correct?");
        }
        hl = new HyperLink("./a", folderOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink("./a/b", folderOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink("../c/b.htm", folderOnSite, someSite);
        assert (!hl.isCurrentPage());
        hl = new HyperLink("./a.htm", folderOnSite, someSite);
        assert (hl.isCurrentPage());
        hl = new HyperLink(".", folderOnSite, someSite);
        assert (hl.isCurrentPage());
        try {
            hl = new HyperLink("../", new URL(someSite, "./abc/xyz.htm"), someSite);
            hl = new HyperLink("xyz-peer.htm", new URL(someSite, "./abc/xyz.htm"), someSite);
            hl = new HyperLink("../", new URL(someSite, "abc/xyz.htm"), someSite);
            hl = new HyperLink("../", new URL(someSite, "./abc"), someSite);
            hl = new HyperLink("../", new URL("http://abc.com/abc"), someSite);
        } catch (MalformedURLException relErrs) {
            err(relErrs.getMessage());
        }
    } catch (MalformedURLException err1) {
        fail(err1.getMessage());
    } catch (Exception err) {
        fail("Developer error -- bad URL." + err.getMessage());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HyperLink(org.opensextant.xtext.collectors.web.HyperLink) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test)

Example 60 with MalformedURLException

use of java.net.MalformedURLException in project OpenAM by OpenRock.

the class PolicyListenerRequest method policyChanged.

/**
     * Handles policy change event.
     *
     * @param evt the policy event regarding the policy change.
     */
public void policyChanged(PolicyEvent evt) {
    debug.message("PolicyListenerRequest.policyChanged()");
    if (evt == null) {
        debug.error("PolicyListenerRequest.policyChanged(PolicyEvent): " + "invalid policy event");
        return;
    }
    // get the policy change type from the event
    String changeType = null;
    int type = evt.getChangeType();
    if (type == PolicyEvent.POLICY_ADDED) {
        changeType = PolicyChangeNotification.ADDED;
    } else if (type == PolicyEvent.POLICY_REMOVED) {
        changeType = PolicyChangeNotification.DELETED;
    } else {
        changeType = PolicyChangeNotification.MODIFIED;
    }
    // get the resource names from the event
    Set resourceNames = evt.getResourceNames();
    if (debug.messageEnabled()) {
        debug.message("PolicyListenerRequest.policyChanged(PolicyEvent): " + "resource names from the policy event : " + resourceNames.toString());
    }
    PolicyService ps = new PolicyService();
    PolicyNotification pn = new PolicyNotification();
    PolicyChangeNotification pcn = new PolicyChangeNotification();
    /*
         * sets the service name and resource names which are affected
         *  by this policy change notification.
         */
    pcn.setResourceNames(resourceNames);
    pcn.setPolicyChangeType(changeType);
    pcn.setServiceName(serviceName);
    pn.setNotificationType(PolicyNotification.POLICY_CHANGE_TYPE);
    pn.setPolicyChangeNotification(pcn);
    ps.setMethodID(PolicyService.POLICY_NOTIFICATION_ID);
    ps.setPolicyNotification(pn);
    /*
         * create a Notification object based on the policy change
         * notification.
         */
    Notification notification = new Notification(ps.toXMLString());
    NotificationSet set = new NotificationSet(PolicyService.POLICY_SERVICE);
    // add the notification to the notification set to be sent to the client
    set.addNotification(notification);
    if (debug.messageEnabled()) {
        debug.message("PolicyListenerRequest.policyChanged(PolicyEvent): " + "the notification set sent is : " + set.toXMLString());
    }
    try {
        // sends the notification to the client
        PLLServer.send(new URL(notificationURL), set);
        if (debug.messageEnabled()) {
            debug.message("PolicyListenerRequest.policyChanged(PolicyEvent): " + "the policy change notification has been sent to " + notificationURL);
        }
    } catch (SendNotificationException e) {
        debug.error("PolicyListenerRequest.policyChanged(): " + "PLLServer.send() failed", e);
    } catch (MalformedURLException e) {
        debug.error("PolicyListenerRequest.policyChanged(): " + "PLLServer.send() failed", e);
    }
}
Also used : NotificationSet(com.iplanet.services.comm.share.NotificationSet) MalformedURLException(java.net.MalformedURLException) NotificationSet(com.iplanet.services.comm.share.NotificationSet) Set(java.util.Set) SendNotificationException(com.iplanet.services.comm.server.SendNotificationException) Notification(com.iplanet.services.comm.share.Notification) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)3838 URL (java.net.URL)2885 IOException (java.io.IOException)1194 File (java.io.File)910 ArrayList (java.util.ArrayList)372 InputStream (java.io.InputStream)367 HttpURLConnection (java.net.HttpURLConnection)295 URISyntaxException (java.net.URISyntaxException)270 URI (java.net.URI)239 InputStreamReader (java.io.InputStreamReader)226 BufferedReader (java.io.BufferedReader)208 HashMap (java.util.HashMap)200 URLClassLoader (java.net.URLClassLoader)168 Map (java.util.Map)166 URLConnection (java.net.URLConnection)148 FileNotFoundException (java.io.FileNotFoundException)137 Matcher (java.util.regex.Matcher)132 Test (org.junit.Test)129 UnsupportedEncodingException (java.io.UnsupportedEncodingException)119 Pattern (java.util.regex.Pattern)113