Example usage for java.net URLConnection connect

List of usage examples for java.net URLConnection connect

Introduction

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

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:org.wso2.carbon.mediation.registry.MicroIntegratorRegistry.java

/**
 * Function to retrieve resource content as text
 *
 * @param url// w w w .j ava  2  s  .  c  om
 * @return
 * @throws IOException
 */
private OMNode readNonXML(URL url) throws IOException {

    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();

    try (InputStream inputStream = urlConnection.getInputStream()) {

        if (inputStream == null) {
            return null;
        }

        StringBuilder strBuilder = new StringBuilder();
        try (BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = bReader.readLine()) != null) {
                strBuilder.append(line);
            }
        }
        return OMAbstractFactory.getOMFactory().createOMText(strBuilder.toString());
    }
}

From source file:org.wso2.carbon.mediation.registry.MicroIntegratorRegistry.java

/**
 * The micro integrator expects the properties of a directory to be available inside the given directory as a
 * property file. For an example, if a directory key, conf:/foo/bar is passed as the key, the micro integrator
 * registry expects the properties to be available in the file, conf:/foo/bar/bar.properties. For a file,
 * conf:/foo/bar/example.xml, the properties need to be given in the file, conf:/foo/bar/example.xml.properties
 *
 * @param key the path to the directory// www  .  j av  a  2s  .c om
 * @return the properties defined
 */
public Properties lookupProperties(String key) {
    if (log.isDebugEnabled()) {
        log.debug("==> Repository fetch of resource with key : " + key);
    }
    String resolvedRegKeyPath = resolveRegistryPath(key);
    Properties result = new Properties();

    URL url = null;
    // get the path to the relevant property file
    try {
        resolvedRegKeyPath = getPropertyFilePath(resolvedRegKeyPath);
        url = new URL(resolvedRegKeyPath);
    } catch (MalformedURLException e) {
        handleException("Invalid path '" + resolvedRegKeyPath + "' for URL", e);
    }

    if (url == null) {
        handleException("Unable to create URL for target resource : " + key);
    }

    if ("file".equals(url.getProtocol())) {
        //Check existence of the file
        try {
            url.openStream();
        } catch (IOException e) {
            if (log.isDebugEnabled()) {
                log.debug("Error occurred while accessing registry resource: " + key, e);
            }
            return null;
        }
    }

    try {
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();
        try (InputStream input = urlConnection.getInputStream()) {
            if (input == null) {
                return null;
            }
            result.load(input);
        }
    } catch (IOException e) {
        log.error("Error in loading properties", e);
        return null;
    }
    return result;
}

From source file:org.apache.hadoop.mapred.task.reduce.Fetcher.java

/**
 * The connection establishment is attempted multiple times and is given up
 * only on the last failure. Instead of connecting with a timeout of X, we
 * try connecting with a timeout of x < X but multiple times.
 *///  w  ww.  j  a  v a  2s .c  o  m
private void connect(URLConnection connection, int connectionTimeout) throws IOException {
    int unit = 0;
    if (connectionTimeout < 0) {
        throw new IOException("Invalid timeout " + "[timeout = " + connectionTimeout + " ms]");
    } else if (connectionTimeout > 0) {
        unit = Math.min(UNIT_CONNECT_TIMEOUT, connectionTimeout);
    }
    // set the connect timeout to the unit-connect-timeout
    connection.setConnectTimeout(unit);
    while (true) {
        try {
            connection.connect();
            break;
        } catch (IOException ioe) {
            // update the total remaining connect-timeout
            connectionTimeout -= unit;

            // throw an exception if we have waited for timeout amount of
            // time
            // note that the updated value if timeout is used here
            if (connectionTimeout == 0) {
                throw ioe;
            }

            // reset the connect timeout for the last try
            if (connectionTimeout < unit) {
                unit = connectionTimeout;
                // reset the connect time out for the final connect
                connection.setConnectTimeout(unit);
            }
        }
    }
}

From source file:com.c4mprod.utils.ImageManager.java

/**
 * /*  w ww.  j a v  a 2  s.  co m*/
 * @param listener
 *            call when download is finished
 * @param url
 *            url of the image to download
 * @param view
 *            image container (put in background if is not an ImageView)
 * @param flags
 * @param checkNewVersionDelay
 *            delay between two checks of the image in milliseconds
 * @param defaultImageId
 *            TODO
 * @param defaultDrawableId
 */
public void download(ImageDownloaderListener listener, final String url, View view, int flags,
        long checkNewVersionDelay, int defaultImageId) {
    if (mStopped || TextUtils.isEmpty(url)) {
        return;
    }
    // mListener = listener;

    // get image from cache
    Bitmap cachedBitmap = null;
    if ((flags & FLAG_GET_THUMBNAIL) != 0) {
        cachedBitmap = mImageLiveCache.get(url + THUMB_FOLDER);
    } else {
        cachedBitmap = mImageLiveCache.get(url);
    }

    ImageDownloadMessageData messageData = new ImageDownloadMessageData(url, view, defaultImageId);
    messageData.listerner = listener;
    DownloadedDrawable downloadedDrawable;
    downloadedDrawable = new DownloadedDrawable(messageData, mContext.getResources(),
            cachedBitmap != null ? cachedBitmap : getDefaultImage(defaultImageId));

    if (view != null) {
        if (view instanceof ImageView) {
            if ((flags & FLAG_IN_BACKGROUND) != 0) {
                view.setBackgroundDrawable(downloadedDrawable);
            } else {
                ((ImageView) view).setImageDrawable(downloadedDrawable);
            }

        } else if (view instanceof ImageButton) {
            if ((flags & FLAG_IN_BACKGROUND) != 0) {
                view.setBackgroundDrawable(downloadedDrawable);
            } else {
                ((ImageButton) view).setImageDrawable(downloadedDrawable);
            }
        } else {
            view.setBackgroundDrawable(downloadedDrawable);
        }
    }

    if (cachedBitmap != null) {
        // Log.d("test", "version form cache");
        Message respMessage = new Message();
        messageData.bitmap = cachedBitmap;
        messageData.flags = flags;
        respMessage.obj = messageData;
        mUiHandler.sendMessage(respMessage);
    } else {
        messageData.flags = flags;

        // check if available from sd card
        if (isSDCacheReadable()) {
            File extCacheDir = mContext.getExternalCacheDir();
            final File img = new File(extCacheDir, md5(url));
            final Long currentDate = Calendar.getInstance().getTimeInMillis();

            if (img.exists()) {

                Message msg = new Message();
                msg.obj = messageData;
                msg.what = MSG_LOAD_FROM_SD;
                mDowloadLooper.enqueueMessage(msg, true);
                // Log.d("test", "version form sdcard");
                if (img.lastModified() + checkNewVersionDelay < currentDate) {
                    final ImageDownloadMessageData test = messageData;
                    Thread thread = new Thread(new Runnable() {

                        @Override
                        public void run() {

                            Message msg = new Message();
                            msg.obj = test;
                            URL connexion;
                            try {

                                connexion = new URL(url);
                                URLConnection urlConnection;
                                urlConnection = connexion.openConnection();
                                urlConnection.connect();
                                String date = urlConnection.getHeaderField("Last-Modified");
                                if (img.lastModified() >= mDateFormater.parse(date).getTime()) {
                                    // Log.d("test", "version form sdcard after server check");
                                    return;
                                } else {
                                    // load from web
                                    // Log.d("test", "version form server");
                                    msg.what = MSG_DOWNLOAD;
                                    mDowloadLooper.enqueueMessage(msg, false);
                                }
                                urlConnection = null;
                            } catch (Exception e) {
                                return;
                            }

                        }
                    });
                    thread.start();

                }
            } else {
                Message msg = new Message();
                msg.obj = messageData;
                // load from web
                // Log.d("test", "version form server");
                msg.what = MSG_DOWNLOAD;
                mDowloadLooper.enqueueMessage(msg, false);
            }
        }

    }
}

From source file:org.sakaiproject.lessonbuildertool.service.AjaxServer.java

public String getMimeType(String url) {
    Session s = SessionManager.getCurrentSession();
    if (s == null || s.getUserId() == null) {
        //       return "";
    }

    if (SimplePageBean.getYoutubeKeyFromUrl(url) != null)
        return "application/youtube";

    String mimeType = "";
    URLConnection conn = null;// w w  w  . j a va  2  s.co  m
    try {
        conn = new URL(new URL(ServerConfigurationService.getServerUrl()), url).openConnection();
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        conn.connect();
        String t = conn.getContentType();
        if (t != null && !t.equals("")) {
            int i = t.indexOf(";");
            if (i >= 0)
                t = t.substring(0, i);
            t = t.trim();
            mimeType = t;
        }
    } catch (Exception e) {
    } finally {
        if (conn != null) {
            try {
                conn.getInputStream().close();
            } catch (Exception e) {
                // log.error("getTypeOfUrl unable to close " + e);
            }
        }
    }

    if (mimeType == null || mimeType.equals("")) {
        String name = url;

        // starts after last /
        int i = name.lastIndexOf("/");
        if (i >= 0)
            name = name.substring(i + 1);

        String extension = null;
        i = name.lastIndexOf(".");
        if (i > 0)
            extension = name.substring(i + 1);

        if (extension == null)
            return "";

        if (SimplePageBean.imageTypes.contains(extension)) {
            return "image/unknown";
        }
        if (extension.equals("html") || extension.equals("htm")) {
            return "text/html";
        } else if (extension.equals("xhtml") || extension.equals("xht")) {
            return "application/xhtml+xml";
        } else {
            return "";
        }

    }

    return mimeType;
}

From source file:com.android.sdklib.repository.legacy.remote.internal.DownloadCache.java

/**
 * Calls {@link HttpConfigurable#openHttpConnection(String)}
 * to actually perform a download.//  ww w  . ja v  a  2  s  . c  om
 * <p>
 * Isolated so that it can be overridden by unit tests.
 */
@VisibleForTesting(visibility = Visibility.PRIVATE)
@NonNull
protected Pair<InputStream, URLConnection> openUrl(@NonNull String url, boolean needsMarkResetSupport,
        @NonNull ITaskMonitor monitor, @Nullable Header[] headers) throws IOException {
    URLConnection connection = new URL(url).openConnection();
    if (headers != null) {
        for (Header header : headers) {
            connection.setRequestProperty(header.getName(), header.getValue());
        }
    }
    connection.connect();
    InputStream is = connection.getInputStream();
    if (needsMarkResetSupport) {
        is = ensureMarkReset(is);
    }

    return Pair.of(is, connection);
}

From source file:bolts.WebViewAppLinkResolver.java

@Override
public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) {
    final Capture<String> content = new Capture<String>();
    final Capture<String> contentType = new Capture<String>();
    return Task.callInBackground(new Callable<Void>() {
        @Override//from   w w w.  j  a  v  a 2  s.  co  m
        public Void call() throws Exception {
            URL currentURL = new URL(url.toString());
            URLConnection connection = null;
            while (currentURL != null) {
                // Fetch the content at the given URL.
                connection = currentURL.openConnection();
                if (connection instanceof HttpURLConnection) {
                    // Unfortunately, this doesn't actually follow redirects if they go from http->https,
                    // so we have to do that manually.
                    ((HttpURLConnection) connection).setInstanceFollowRedirects(true);
                }
                connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX);
                connection.connect();

                if (connection instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) {
                        currentURL = new URL(httpConnection.getHeaderField("Location"));
                        httpConnection.disconnect();
                    } else {
                        currentURL = null;
                    }
                } else {
                    currentURL = null;
                }
            }

            try {
                content.set(readFromConnection(connection));
                contentType.set(connection.getContentType());
            } finally {
                if (connection instanceof HttpURLConnection) {
                    ((HttpURLConnection) connection).disconnect();
                }
            }
            return null;
        }
    }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() {
        @Override
        public Task<JSONArray> then(Task<Void> task) throws Exception {
            // Load the content in a WebView and use JavaScript to extract the meta tags.
            final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>();
            final WebView webView = new WebView(context);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setNetworkAvailable(false);
            webView.setWebViewClient(new WebViewClient() {
                private boolean loaded = false;

                private void runJavaScript(WebView view) {
                    if (!loaded) {
                        // After the first resource has been loaded (which will be the pre-populated data)
                        // run the JavaScript meta tag extraction script
                        loaded = true;
                        view.loadUrl(TAG_EXTRACTION_JAVASCRIPT);
                    }
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    runJavaScript(view);
                }

                @Override
                public void onLoadResource(WebView view, String url) {
                    super.onLoadResource(view, url);
                    runJavaScript(view);
                }
            });
            // Inject an object that will receive the JSON for the extracted JavaScript tags
            webView.addJavascriptInterface(new Object() {
                @JavascriptInterface
                public void setValue(String value) {
                    try {
                        tcs.trySetResult(new JSONArray(value));
                    } catch (JSONException e) {
                        tcs.trySetError(e);
                    }
                }
            }, "boltsWebViewAppLinkResolverResult");
            String inferredContentType = null;
            if (contentType.get() != null) {
                inferredContentType = contentType.get().split(";")[0];
            }
            webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null);
            return tcs.getTask();
        }
    }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() {
        @Override
        public AppLink then(Task<JSONArray> task) throws Exception {
            Map<String, Object> alData = parseAlData(task.getResult());
            AppLink appLink = makeAppLinkFromAlData(alData, url);
            return appLink;
        }
    });
}

From source file:org.seasar.struts.hotdeploy.impl.ModuleConfigLoaderImpl.java

protected void parseModuleConfigFile(Digester digester, URL url) throws UnavailableException {
    InputStream input = null;//ww w  .  j  a va  2 s . c o  m

    try {
        InputSource is = new InputSource(url.toExternalForm());
        URLConnection conn = url.openConnection();

        conn.setUseCaches(false);
        conn.connect();
        input = conn.getInputStream();
        is.setByteStream(input);
        digester.parse(is);
    } catch (IOException e) {
        handleConfigException(url.toString(), e);
    } catch (SAXException e) {
        handleConfigException(url.toString(), e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                throw new UnavailableException(e.getMessage());
            }
        }
    }
}

From source file:youtube.transcription.Video.java

public InputStreamReader readURL(String s) throws MalformedURLException, IOException {
    URL url;//from   w  ww.j a va  2 s .c o  m
    InputStreamReader isr;
    String appName, appVersion;
    URLConnection urlconn;

    appName = java.util.ResourceBundle.getBundle("Bundle").getString("app.name");
    appVersion = java.util.ResourceBundle.getBundle("Bundle").getString("app.version");

    url = new URL(s);
    if (proxy != null) {
        urlconn = url.openConnection(proxy);
    } else {
        urlconn = url.openConnection();
    }
    urlconn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    urlconn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; " + appName + "/" + appVersion + ")");
    urlconn.connect();

    isr = new InputStreamReader(urlconn.getInputStream(), "UTF-8");
    return isr;
}

From source file:org.adaway.service.UpdateService.java

/**
 * Check for updates of hosts sources//from   w ww .  ja  v a  2 s  .co m
 *
 * @return return code
 */
private int checkForUpdates() {
    Cursor enabledHostsSourcesCursor;
    long currentLastModifiedLocal;
    long currentLastModifiedOnline;
    boolean updateAvailable = false;

    int returnCode = StatusCodes.ENABLED; // default return code

    if (Utils.isAndroidOnline(mService)) {

        mNumberOfFailedDownloads = 0;
        mNumberOfDownloads = 0;

        // get cursor over all enabled hosts source
        enabledHostsSourcesCursor = ProviderHelper.getEnabledHostsSourcesCursor(mService);

        // iterate over all hosts sources in db with cursor
        if (enabledHostsSourcesCursor != null && enabledHostsSourcesCursor.moveToFirst()) {
            do {

                mNumberOfDownloads++;

                // get url and lastModified from db
                String currentUrl = enabledHostsSourcesCursor
                        .getString(enabledHostsSourcesCursor.getColumnIndex("url"));
                currentLastModifiedLocal = enabledHostsSourcesCursor
                        .getLong(enabledHostsSourcesCursor.getColumnIndex("last_modified_local"));

                try {
                    Log.v(Constants.TAG, "Checking hosts file: " + currentUrl);

                    /* build connection */
                    URL mURL = new URL(currentUrl);
                    URLConnection connection = mURL.openConnection();
                    connection.setConnectTimeout(15000);
                    connection.setReadTimeout(30000);

                    currentLastModifiedOnline = connection.getLastModified();

                    Log.d(Constants.TAG, "mConnectionLastModified: " + currentLastModifiedOnline + " ("
                            + DateUtils.longToDateString(mService, currentLastModifiedOnline) + ")");

                    Log.d(Constants.TAG, "mCurrentLastModified: " + currentLastModifiedLocal + " ("
                            + DateUtils.longToDateString(mService, currentLastModifiedLocal) + ")");

                    // check if file is available
                    connection.connect();
                    connection.getInputStream();

                    // check if update available for this hosts file
                    if (currentLastModifiedOnline > currentLastModifiedLocal) {
                        updateAvailable = true;
                    }

                    // save last modified online for later viewing in list
                    ProviderHelper.updateHostsSourceLastModifiedOnline(mService,
                            enabledHostsSourcesCursor
                                    .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)),
                            currentLastModifiedOnline);

                } catch (Exception e) {
                    Log.e(Constants.TAG, "Exception while downloading from " + currentUrl, e);

                    mNumberOfFailedDownloads++;

                    // set last_modified_online of failed download to 0 (not available)
                    ProviderHelper.updateHostsSourceLastModifiedOnline(mService, enabledHostsSourcesCursor
                            .getInt(enabledHostsSourcesCursor.getColumnIndex(HostsSources._ID)), 0);
                }

            } while (enabledHostsSourcesCursor.moveToNext());
        }

        // close cursor in the end
        if (enabledHostsSourcesCursor != null && !enabledHostsSourcesCursor.isClosed()) {
            enabledHostsSourcesCursor.close();
        }

        // if all downloads failed return download_fail error
        if (mNumberOfDownloads == mNumberOfFailedDownloads && mNumberOfDownloads != 0) {
            returnCode = StatusCodes.DOWNLOAD_FAIL;
        }
    } else {
        // only report no connection when not in background
        if (!mBackgroundExecution) {
            returnCode = StatusCodes.NO_CONNECTION;
        } else {
            Log.e(Constants.TAG, "Should not happen! In background execution is no connection available!");
        }
    }

    // set return code if update is available
    if (updateAvailable) {
        returnCode = StatusCodes.UPDATE_AVAILABLE;
    }

    // check if hosts file is applied
    if (!ApplyUtils.isHostsFileCorrect(mService, Constants.ANDROID_SYSTEM_ETC_HOSTS)) {
        returnCode = StatusCodes.DISABLED;
    }

    return returnCode;
}