Example usage for java.net MalformedURLException MalformedURLException

List of usage examples for java.net MalformedURLException MalformedURLException

Introduction

In this page you can find the example usage for java.net MalformedURLException MalformedURLException.

Prototype

public MalformedURLException(String msg) 

Source Link

Document

Constructs a MalformedURLException with the specified detail message.

Usage

From source file:com.googlecode.fightinglayoutbugs.DetectInvalidImageUrls.java

private void checkDataUrl(String url) throws MalformedURLException {
    if (!url.startsWith("data:image/")) {
        throw new MalformedURLException("Data URL does not contain image data.");
    }//from   w  ww.j  a  v a  2s.  c  o  m
    // TODO: check if the data URL contains a valid image.
}

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

public void getRawTextFromUrlIfNeeded(DocumentPojo doc, SourceRssConfigPojo feedConfig) throws IOException {
    if (null != doc.getFullText()) { // Nothing to do
        return;/*from  w ww. j av a2s.  co  m*/
    }
    Scanner s = null;
    OutputStreamWriter wr = null;
    try {
        URL url = new URL(doc.getUrl());
        URLConnection urlConnect = null;
        String postContent = null;
        if (null != feedConfig) {
            urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
            if (null != feedConfig.getUserAgent()) {
                urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
            } // TESTED (by hand)
            if (null != feedConfig.getHttpFields()) {
                for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                    if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                        postContent = httpFieldPair.getValue();
                        urlConnect.setDoInput(true);
                        urlConnect.setDoOutput(true);
                    } else {
                        urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                    }
                }
            } //TESTED (by hand)
        } else {
            urlConnect = url.openConnection();
        }
        InputStream urlStream = null;
        try {
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } catch (SecurityException se) {
            throw se;
        } catch (Exception e) { // Try one more time, this time exception out all the way
            securityManager.setSecureFlag(false); // (some file stuff - so need to re-enable)
            if (null != feedConfig) {
                urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
                if (null != feedConfig.getUserAgent()) {
                    urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
                } // TESTED
                if (null != feedConfig.getHttpFields()) {
                    for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                        if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                            urlConnect.setDoInput(true); // (need to do this again)
                            urlConnect.setDoOutput(true);
                        } else {
                            urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                        }
                    }
                } //TESTED
            } else {
                urlConnect = url.openConnection();
            }
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } finally {
            securityManager.setSecureFlag(false); // (turn security check for local URL/file access off)
        }
        // Grab any interesting header fields
        Map<String, List<String>> headers = urlConnect.getHeaderFields();
        BasicDBObject metadataHeaderObj = null;
        for (Map.Entry<String, List<String>> it : headers.entrySet()) {
            if (null != it.getKey()) {
                if (it.getKey().startsWith("X-") || it.getKey().startsWith("Set-")
                        || it.getKey().startsWith("Location")) {
                    if (null == metadataHeaderObj) {
                        metadataHeaderObj = new BasicDBObject();
                    }
                    metadataHeaderObj.put(it.getKey(), it.getValue());
                }
            }
        } //TESTED
          // Grab the response code
        try {
            HttpURLConnection httpUrlConnect = (HttpURLConnection) urlConnect;
            int responseCode = httpUrlConnect.getResponseCode();
            if (200 != responseCode) {
                if (null == metadataHeaderObj) {
                    metadataHeaderObj = new BasicDBObject();
                }
                metadataHeaderObj.put("responseCode", String.valueOf(responseCode));
            }
        } //TESTED
        catch (Exception e) {
        } // interesting, not an HTTP connect ... shrug and carry on
        if (null != metadataHeaderObj) {
            doc.addToMetadata("__FEED_METADATA__", metadataHeaderObj);
        } //TESTED
        s = new Scanner(urlStream, "UTF-8");
        doc.setFullText(s.useDelimiter("\\A").next());
    } catch (MalformedURLException me) { // This one is worthy of a more useful error message
        throw new MalformedURLException(me.getMessage()
                + ": Likely because the document has no full text (eg JSON) and you are calling a contentMetadata block without setting flags:'m' or 'd'");
    } finally { //(release resources)
        if (null != s) {
            s.close();
        }
        if (null != wr) {
            wr.close();
        }
    }

}

From source file:org.couchpotato.CouchPotato.java

private <T> T command(String command, String arguments, Type type)
        throws MalformedURLException, IOException, SocketTimeoutException {
    try {/*  w  ww. j ava2s .  c o m*/
        URI uri = this.getUri(command, arguments);
        // this WILL throw a EOFexception if it gets a HTTP 301 response because it wont follow it
        // so ALL URLS MUST BE PERFECT!!!!
        HttpURLConnection server = (HttpURLConnection) uri.toURL().openConnection();
        // TODO going to try and not use this and see if everything works out
        //         if ( uri.getScheme().compareTo("https") == 0 ) {
        //            server = (HttpsURLConnection)uri.toURL().openConnection();
        //         } else {
        //            server = (HttpURLConnection)uri.toURL().openConnection();
        //         }
        server.setConnectTimeout(SOCKET_TIMEOUT);
        Reader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));
        // TypeToken cannot figure out T so instead it must be supplied
        GsonBuilder build = new GsonBuilder();
        build.registerTypeAdapter(JsonBoolean.class, new JsonBooleanDeserializer());
        T response;
        if (type == MovieListJson.class) {
            response = (T) build.create().fromJson(reader, _NewAPI_MovieListJson.class).toOld();
        } else {
            response = build.create().fromJson(reader, type);
        }
        return response;
    } catch (URISyntaxException e) {
        throw new MalformedURLException(e.getMessage());
    }
}

From source file:mobi.jenkinsci.ci.JenkinsCIPlugin.java

private String getJobPathPart(final String jobUrlString, final String jenkinsUrlString)
        throws MalformedURLException {
    final String jobPath = getJobPath(jobUrlString, jenkinsUrlString);

    final Matcher jobMatch = JOB_PATTERN.matcher(jobPath);
    if (!jobMatch.find()) {
        throw new MalformedURLException("Cannot find job name in path " + jobPath);
    }/*from ww w. ja  v  a2s.c  om*/

    return jobMatch.group(1);
}

From source file:mobi.jenkinsci.ci.JenkinsCIPlugin.java

private String getJobPath(final String jobUrlString, final String jenkinsUrlString)
        throws MalformedURLException {
    String jobPath;//  www  . java  2  s .  c o  m
    final URL jobUrl = new URL(jobUrlString);
    final URL jenkinsUrl = new URL(jenkinsUrlString);
    if (!isSameHost(jobUrl, jenkinsUrl)) {
        throw new MalformedURLException(
                "URLs " + jobUrlString + " and Jenkins " + jenkinsUrlString + " have a different hostname");
    }

    jobPath = jobUrl.getPath();
    final String jenkinsPath = jenkinsUrl.getPath();
    if (!jobPath.startsWith(jenkinsPath)) {
        throw new MalformedURLException("Job path " + jobPath + " is not within Jenkins path " + jenkinsPath);
    }
    return jobPath;
}

From source file:com.janoz.usenet.searchers.impl.NewzbinConnectorImpl.java

private URI constructURL(String path) throws MalformedURLException {

    try {// w w w. jav  a  2  s .c o m
        return new URL(serverProtocol, serverAddress, serverPort, path).toURI();
    } catch (URISyntaxException e) {
        throw new MalformedURLException(e.getMessage());
    }
}

From source file:org.lockss.util.UrlUtil.java

/** Normalize the path component.  Replaces multiple consecutive "/" with
 * a single "/", removes "." components and resolves ".."  components.
 * @param path the path to normalize//from  w  w  w . j av  a 2s.  c o m
 * @param pathTraversalAction what to do if extra ".." components, see
 * {@link #PARAM_PATH_TRAVERSAL_ACTION}.
 */
public static String normalizePath(String path, int pathTraversalAction) throws MalformedURLException {
    path = path.trim();
    // special case compatability with Java 1.4 URI
    if (path.equals(".") || path.equals("./")) {
        return "";
    }
    path = normalizeUrlEncodingCase(path);

    // quickly determine whether anything needs to be done
    if (!(path.endsWith("/.") || path.endsWith("/..") || path.equals("..") || path.equals(".")
            || path.startsWith("../") || path.startsWith("./") || path.indexOf("/./") >= 0
            || path.indexOf("/../") >= 0 || path.indexOf("//") >= 0)) {
        return path;
    }

    StringTokenizer st = new StringTokenizer(path, "/");
    List names = new ArrayList();
    int dotdotcnt = 0;
    boolean prevdotdot = false;
    while (st.hasMoreTokens()) {
        String comp = st.nextToken();
        prevdotdot = false;
        if (comp.equals(".")) {
            continue;
        }
        if (comp.equals("..")) {
            if (names.size() > 0) {
                names.remove(names.size() - 1);
                prevdotdot = true;
            } else {
                switch (pathTraversalAction) {
                case PATH_TRAVERSAL_ACTION_THROW:
                    throw new MalformedURLException("Illegal dir traversal: " + path);
                case PATH_TRAVERSAL_ACTION_ALLOW:
                    dotdotcnt++;
                    break;
                case PATH_TRAVERSAL_ACTION_REMOVE:
                    break;
                }
            }
        } else {
            names.add(comp);
        }
    }

    StringBuilder sb = new StringBuilder();
    if (path.startsWith("/")) {
        sb.append("/");
    }
    for (int ix = dotdotcnt; ix > 0; ix--) {
        if (ix > 1 || !names.isEmpty()) {
            sb.append("../");
        } else {
            sb.append("..");
        }
    }
    StringUtil.separatedString(names, "/", sb);
    if ((path.endsWith("/") || (prevdotdot && !names.isEmpty()))
            && !(sb.length() == 1 && path.startsWith("/"))) {
        sb.append("/");
    }
    return sb.toString();
}

From source file:org.opennms.netmgt.provision.service.dns.DnsRequisitionUrlConnection.java

/**
 * Validate the format is://  w w  w .  ja  v a2 s .c o  m
 *   dns://<host>/<zone>/?expression=<regex>
 *
 *   there should be only one arguement in the path
 *   there should only be one query parameter
 *
 * @param url a {@link java.net.URL} object.
 * @throws java.net.MalformedURLException if any.
 */
protected static void validateDnsUrl(URL url) throws MalformedURLException {

    String path = url.getPath();
    path = StringUtils.removeStart(path, "/");
    path = StringUtils.removeEnd(path, "/");

    if (path == null || StringUtils.countMatches(path, "/") > 1) {
        throw new MalformedURLException("The specified DNS URL contains invalid path: " + url);
    }

    String query = url.getQuery();

    if ((query != null) && (determineExpressionFromUrl(url) == null) && (getArgs().get(SERVICES_ARG) == null)
            && (getArgs().get(FID_HASH_SRC_ARG) == null)) {
        throw new MalformedURLException("The specified DNS URL contains an invalid query string: " + url);
    }

}

From source file:org.hyperic.hq.product.jmx.MxUtil.java

public static JMXConnector getMBeanConnector(Properties config) throws MalformedURLException, IOException {

    String jmxUrl = config.getProperty(MxUtil.PROP_JMX_URL);
    Map map = new HashMap();

    String user = config.getProperty(PROP_JMX_USERNAME);
    String pass = config.getProperty(PROP_JMX_PASSWORD);

    map.put(JMXConnector.CREDENTIALS, new String[] { user, pass });

    // required for Oracle AS
    String providerPackages = config.getProperty(PROP_JMX_PROVIDER_PKGS);
    if (providerPackages != null)
        map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, providerPackages);

    if (jmxUrl == null) {
        throw new MalformedURLException(PROP_JMX_URL + "==null");
    }//from  ww  w  .  ja v  a  2  s . c  o  m

    if (jmxUrl.startsWith(PTQL_PREFIX)) {
        jmxUrl = getUrlFromPid(jmxUrl.substring(PTQL_PREFIX.length()));
    }

    JMXServiceURL url = new JMXServiceURL(jmxUrl);

    String proto = url.getProtocol();
    if (proto.equals("t3") || proto.equals("t3s")) {
        //http://edocs.bea.com/wls/docs92/jmx/accessWLS.html
        //WebLogic support, requires:
        //cp $WLS_HOME/server/lib/wljmxclient.jar pdk/lib/
        //cp $WLS_HOME/server/lib/wlclient.jar pdk/lib/
        map.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
        map.put(Context.SECURITY_PRINCIPAL, user);
        map.put(Context.SECURITY_CREDENTIALS, pass);
    }

    JMXConnector connector = JMXConnectorFactory.connect(url, map);
    if (log.isDebugEnabled()) {
        log.debug("created new JMXConnector url=" + url + ", classloader="
                + Thread.currentThread().getContextClassLoader());
    }
    return connector;
}

From source file:com.bytelightning.opensource.pokerface.ScriptHelperImpl.java

/**
 * Normalization code courtesy of 'Mike Houston' http://stackoverflow.com/questions/2993649/how-to-normalize-a-url-in-java
 */// w w w.  ja v a2  s  .c  om
public static String NormalizeURL(final String taintedURL) throws MalformedURLException {
    final URL url;
    try {
        url = new URI(taintedURL).normalize().toURL();
    } catch (URISyntaxException e) {
        throw new MalformedURLException(e.getMessage());
    }

    final String path = url.getPath().replace("/$", "");
    final SortedMap<String, String> params = CreateParameterMap(url.getQuery());
    final int port = url.getPort();
    final String queryString;

    if (params != null) {
        // Some params are only relevant for user tracking, so remove the most commons ones.
        for (Iterator<String> i = params.keySet().iterator(); i.hasNext();) {
            final String key = i.next();
            if (key.startsWith("utm_") || key.contains("session"))
                i.remove();
        }
        queryString = "?" + Canonicalize(params);
    } else
        queryString = "";

    return url.getProtocol() + "://" + url.getHost() + (port != -1 && port != 80 ? ":" + port : "") + path
            + queryString;
}