Search in sources :

Example 31 with MalformedURLException

use of java.net.MalformedURLException in project platformlayer by platformlayer.

the class ITGitService method testGitRepo.

private void testGitRepo(String repoUrl, String username, String password) throws MalformedURLException {
    String fetchUrl = repoUrl;
    URL url = new URL(fetchUrl);
    // TODO: Create an LDAP user
    System.out.println("Warning - not properly checking git repo");
    try {
        IoUtils.readAll(url);
    } catch (Exception e) {
        // We're not authenticated, so we expect a 401
        System.out.println(e);
        String message = e.getMessage();
        Assert.assertTrue(message.contains("HTTP response code: 401"));
    }
}
Also used : URL(java.net.URL) MalformedURLException(java.net.MalformedURLException)

Example 32 with MalformedURLException

use of java.net.MalformedURLException in project platformlayer by platformlayer.

the class GwtCodegenMojo method buildClassloader.

private URLClassLoader buildClassloader() throws MojoExecutionException {
    List<URL> urlList = new ArrayList<URL>();
    URL[] urls;
    try {
        File targetClasses = new File(outputDirectory, "classes");
        urlList.add(targetClasses.toURL());
        for (String element : runtimeClasspathElements) {
            getLog().info("Adding " + element);
            try {
                urlList.add(new File(element).toURI().toURL());
            } catch (MalformedURLException e) {
                throw new MojoExecutionException("Unable to access project dependency: " + element, e);
            }
        }
        urls = urlList.toArray(new URL[urlList.size()]);
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Error building URLs", e);
    }
    return new URLClassLoader(urls);
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL)

Example 33 with MalformedURLException

use of java.net.MalformedURLException in project grails-core by grails.

the class GrailsResourceUtils method getAppDir.

public static Resource getAppDir(Resource resource) {
    if (resource == null)
        return null;
    try {
        File file = resource.getFile();
        while (file != null && !file.getName().equals(GRAILS_APP_DIR)) {
            file = file.getParentFile();
        }
        if (file != null) {
            return new FileSystemResource(file.getAbsolutePath() + '/');
        }
    } catch (IOException e) {
    }
    try {
        String url = resource.getURL().toString();
        int i = url.lastIndexOf(GRAILS_APP_DIR);
        if (i > -1) {
            url = url.substring(0, i + 10);
            return new UrlResource(url + '/');
        }
        return null;
    } catch (MalformedURLException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) File(java.io.File)

Example 34 with MalformedURLException

use of java.net.MalformedURLException in project physical-web by google.

the class PwsClient method resolve.

/**
   * Send an HTTP request to the PWS to resolve a set of URLs.
   * @param broadcastUrls The URLs to resolve.
   * @param pwsResultCallback The callback to be run when the response is received.
   */
public void resolve(final Collection<String> broadcastUrls, final PwsResultCallback pwsResultCallback) {
    // Create the response callback.
    final long startTime = new Date().getTime();
    JsonObjectRequest.RequestCallback requestCallback = new JsonObjectRequest.RequestCallback() {

        private void recordResponse() {
            pwsResultCallback.onResponseReceived(new Date().getTime() - startTime);
        }

        private PwsResult getPwsResult(JSONObject jsonUrlMetadata) {
            switch(apiVersion) {
                case 1:
                    return getV1PwsResult(jsonUrlMetadata);
                case 2:
                    return getV2PwsResult(jsonUrlMetadata);
                default:
                    throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
            }
        }

        private PwsResult getV1PwsResult(JSONObject jsonUrlMetadata) {
            try {
                return new PwsResult.Builder(jsonUrlMetadata.getString("id"), jsonUrlMetadata.getString("url")).setTitle(jsonUrlMetadata.optString("title")).setDescription(jsonUrlMetadata.optString("description")).setIconUrl(jsonUrlMetadata.optString("icon")).setGroupId(jsonUrlMetadata.optString("groupId")).build();
            } catch (JSONException e) {
                return null;
            }
        }

        private PwsResult getV2PwsResult(JSONObject jsonUrlMetadata) {
            try {
                JSONObject jsonPageInfo = jsonUrlMetadata.getJSONObject("pageInfo");
                return new PwsResult.Builder(jsonUrlMetadata.getString("scannedUrl"), jsonUrlMetadata.getString("resolvedUrl")).setTitle(jsonPageInfo.optString("title")).setDescription(jsonPageInfo.optString("description")).setIconUrl(jsonPageInfo.optString("icon")).build();
            } catch (JSONException e) {
                return null;
            }
        }

        public void onResponse(JSONObject result) {
            recordResponse();
            // Build the metadata from the response.
            JSONArray foundMetadata;
            String jsonKey;
            switch(apiVersion) {
                case 1:
                    jsonKey = "metadata";
                    break;
                case 2:
                    jsonKey = "results";
                    break;
                default:
                    throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
            }
            try {
                foundMetadata = result.getJSONArray(jsonKey);
            } catch (JSONException e) {
                pwsResultCallback.onPwsResultError(broadcastUrls, 200, e);
                return;
            }
            // Loop through the metadata for each url.
            Set<String> foundUrls = new HashSet<>();
            for (int i = 0; i < foundMetadata.length(); i++) {
                JSONObject jsonUrlMetadata = foundMetadata.getJSONObject(i);
                PwsResult pwsResult = getPwsResult(jsonUrlMetadata);
                pwsResultCallback.onPwsResult(pwsResult);
                foundUrls.add(pwsResult.getRequestUrl());
            }
            // See which urls the PWS didn't give us a response for.
            Set<String> missed = new HashSet<>(broadcastUrls);
            missed.removeAll(foundUrls);
            for (String url : missed) {
                pwsResultCallback.onPwsResultAbsent(url);
            }
        }

        public void onError(int responseCode, Exception e) {
            recordResponse();
            pwsResultCallback.onPwsResultError(broadcastUrls, responseCode, e);
        }
    };
    // Create the request.
    String targetUrl = constructPwsResolveUrl();
    JSONObject payload = new JSONObject();
    try {
        JSONArray urls = new JSONArray();
        for (String url : broadcastUrls) {
            JSONObject obj = new JSONObject();
            obj.put("url", url);
            urls.put(obj);
        }
        String jsonKey;
        switch(apiVersion) {
            case 1:
                jsonKey = "objects";
                break;
            case 2:
                jsonKey = "urls";
                break;
            default:
                throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
        }
        payload.put(jsonKey, urls);
    } catch (JSONException e) {
        pwsResultCallback.onPwsResultError(broadcastUrls, 0, e);
        return;
    }
    Request request;
    try {
        request = new JsonObjectRequest(targetUrl, payload, requestCallback);
    } catch (MalformedURLException e) {
        pwsResultCallback.onPwsResultError(broadcastUrls, 0, e);
        return;
    }
    makeRequest(request);
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Date(java.util.Date) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException) JSONObject(org.json.JSONObject) HashSet(java.util.HashSet)

Example 35 with MalformedURLException

use of java.net.MalformedURLException in project android-maps-utils by googlemaps.

the class HeatmapsPlacesDemoActivity method getJsonPlaces.

/**
     * Makes a radar search request and returns the results in a json format.
     *
     * @param keyword  The keyword to be searched for.
     * @param location The location the radar search should be based around.
     * @return The results from the radar search request as a json
     */
private String getJsonPlaces(String keyword, LatLng location) {
    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        URL url = new URL(PLACES_API_BASE + TYPE_RADAR_SEARCH + OUT_JSON + "?location=" + location.latitude + "," + location.longitude + "&radius=" + (SEARCH_RADIUS / 2) + "&sensor=false" + "&key=" + API_KEY + "&keyword=" + keyword.replace(" ", "%20"));
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());
        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        Toast.makeText(this, "Error processing Places API URL", Toast.LENGTH_SHORT).show();
        return null;
    } catch (IOException e) {
        Toast.makeText(this, "Error connecting to Places API", Toast.LENGTH_SHORT).show();
        return null;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    return jsonResults.toString();
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

MalformedURLException (java.net.MalformedURLException)1597 URL (java.net.URL)1204 IOException (java.io.IOException)482 File (java.io.File)405 ArrayList (java.util.ArrayList)168 InputStream (java.io.InputStream)135 HttpURLConnection (java.net.HttpURLConnection)131 HashMap (java.util.HashMap)100 URISyntaxException (java.net.URISyntaxException)98 URI (java.net.URI)95 Map (java.util.Map)80 URLClassLoader (java.net.URLClassLoader)77 InputStreamReader (java.io.InputStreamReader)75 BufferedReader (java.io.BufferedReader)72 URLConnection (java.net.URLConnection)62 FileNotFoundException (java.io.FileNotFoundException)59 Test (org.junit.Test)54 HashSet (java.util.HashSet)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)44 JSONException (org.json.JSONException)40