Example usage for java.net URLEncoder encode

List of usage examples for java.net URLEncoder encode

Introduction

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

Prototype

public static String encode(String s, Charset charset) 

Source Link

Document

Translates a string into application/x-www-form-urlencoded format using a specific java.nio.charset.Charset Charset .

Usage

From source file:io.github.retz.mesosc.MesosHTTPFetcher.java

public static Optional<String> sandboxDownloadUri(String master, String slaveId, String frameworkId,
        String executorId, String containerId, String path) {
    Optional<String> base = sandboxUri("download", master, slaveId, frameworkId, executorId, containerId,
            RETRY_LIMIT);//from w  w w.j a va  2s . c  o  m
    if (base.isPresent()) {
        try {
            String encodedPath = URLEncoder.encode(path, UTF_8.toString());
            return Optional.of(base.get() + encodedPath);
        } catch (UnsupportedEncodingException e) {
            return Optional.empty();
        }
    }
    return Optional.empty();
}

From source file:com.kolich.blog.entities.EntryTag.java

/**
 * Helper method to URL encode the incoming tag.
 *///from  w w  w.  j  a  va  2 s .c o m
public static final String encodeTag(final String tag) {
    try {
        return URLEncoder.encode(tag, UTF_8_CHARSET);
    } catch (Exception e) {
        // Ugh, is this really even possible on modern stacks?
        throw new RuntimeException("Failed miserably to UTF-8 URL encode entry tag: " + tag, e);
    }
}

From source file:io.knotx.adapter.common.placeholders.UriTransformer.java

private static String encodeValue(String value) {
    try {//from ww  w .ja  va  2 s.  c o  m
        return URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("%2F", "/");
    } catch (UnsupportedEncodingException ex) {
        LOGGER.fatal("Unexpected Exception - Unsupported encoding UTF-8", ex);
        throw new UnsupportedCharsetException("UTF-8");
    }
}

From source file:com.aperigeek.facebook.fmap.gmapi.GMapsGeocodingClient.java

public GeoLocation geocode(String address) throws GMapsException {
    try {/*  ww w  .j  a  va  2 s  .c  o  m*/
        String gmurl = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="
                + URLEncoder.encode(address, GMAPS_CHARSET);

        URL url = new URL(gmurl);

        InputStream in = url.openStream();
        InputStreamReader reader = new InputStreamReader(in);

        JSONTokener tokener = new JSONTokener(reader);

        JSONObject object = (JSONObject) tokener.nextValue();
        String status = object.getString("status");
        if (status.equals("ZERO_RESULTS")) {
            return null;
        }
        if (!status.equals("OK")) {
            throw new GMapsException("Error during request: " + status);
        }

        JSONArray results = object.getJSONArray("results");
        JSONObject result = results.getJSONObject(0);
        JSONObject geometry = result.getJSONObject("geometry");
        JSONObject jsonLocation = geometry.getJSONObject("location");

        GeoLocation location = new GeoLocation(jsonLocation.getDouble("lat"), jsonLocation.getDouble("lng"));

        in.close();

        return location;
    } catch (IOException ex) {
        throw new GMapsException("Unexpectec I/O error", ex);
    } catch (JSONException ex) {
        throw new GMapsException("API error: invalid JSON", ex);
    }
}

From source file:com.yilang.commons.utils.util.Encodes.java

public static String urlEncode(String part) {
    try {/*from   w w  w.  j a  va2  s . c  om*/
        return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:org.jasig.portlet.campuslife.service.UPortalURLServiceImpl.java

@Override
public String getLocationUrl(String identifier, PortletRequest request) {
    try {/*from w w  w . java  2s.c  o  m*/
        final String encodedLocation = URLEncoder.encode(identifier, "UTF-8");
        return portalContext.concat("/s/location?id=").concat(encodedLocation);
    } catch (UnsupportedEncodingException e) {
        log.error("Unable to encode location id " + identifier);
        return null;
    }
}

From source file:com.aperigeek.facebook.fmap.geo.osm.NominatimGeocodingClient.java

public GeoLocation geocode(String address) throws GeoException {
    try {//from  w w w  .j  av a 2  s .  c  o m
        String osmurl = "http://nominatim.openstreetmap.org/search?format=json&q="
                + URLEncoder.encode(address, NOMINATIM_CHARSET);

        URL url = new URL(osmurl);

        InputStream in = url.openStream();
        InputStreamReader reader = new InputStreamReader(in);
        JSONTokener tokener = new JSONTokener(reader);

        JSONArray results = (JSONArray) tokener.nextValue();

        if (results.length() == 0) {
            System.out.println("No results for: " + address);
            return null;
        }

        JSONObject result = results.getJSONObject(0);

        return new GeoLocation(result.getDouble("lat"), result.getDouble("lon"));
    } catch (IOException ex) {
        throw new GeoException("Unexpected I/O error", ex);
    } catch (JSONException ex) {
        throw new GeoException("API error: invalid JSON message", ex);
    }
}

From source file:com.amgems.uwschedule.util.NetUtils.java

/**
 * Builds a HTTP compliant query string from a series of NameValuePairs.
 *//*from   w  w w  .j  a  v a  2s  . c om*/
public static String toQueryString(List<? extends NameValuePair> postParameterPairs) {
    StringBuilder builder = new StringBuilder();
    boolean firstParameter = true;

    try {
        for (NameValuePair postParameterPair : postParameterPairs) {
            if (!firstParameter)
                builder.append("&");
            firstParameter = false;

            builder.append(URLEncoder.encode(postParameterPair.getName(), NetUtils.CHARSET));
            builder.append("=");
            builder.append(URLEncoder.encode(postParameterPair.getValue(), NetUtils.CHARSET));
        }
    } catch (UnsupportedEncodingException e) {
        Log.e(NetUtils.class.getSimpleName(), e.getMessage());
    }

    return builder.toString();
}

From source file:com.casker.portfolio.controller.FileController.java

/**
 *  /*from  w w w . ja v a 2s  .c o  m*/
 * 
 * @return
 */
@ResponseBody
@RequestMapping("/portfolio/{portfolioNo}/{imageType}")
public void editPassword(HttpServletResponse response, @PathVariable long portfolioNo,
        @PathVariable String imageType) throws Exception {

    Portfolio portfolio = portfolioService.getPortfolioDetail(portfolioNo);

    File file = portfolioService.getImageFile(portfolio, imageType);

    response.setContentLength((int) file.length());

    String fileName = URLEncoder.encode(file.getName(), "utf-8");

    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
    response.setHeader("Content-Transfer-Encoding", "binary");

    InputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, response.getOutputStream());
    IOUtils.closeQuietly(inputStream);

    response.flushBuffer();
}

From source file:net.praqma.jenkins.rqm.model.TestSuiteExecutionRecord.java

public static String getResourceFeedUrl(String host, int port, String context, String project)
        throws UnsupportedEncodingException {
    String request = String.format(
            "%s:%s/%s/service/com.ibm.rqm.integration.service.IIntegrationService/resources/%s/%s", host, port,
            context, URLEncoder.encode(project, "UTF-8"), RESOURCE_RQM_NAME);
    return request;
}