Example usage for java.net URLDecoder decode

List of usage examples for java.net URLDecoder decode

Introduction

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

Prototype

public static String decode(String s, Charset charset) 

Source Link

Document

Decodes an application/x-www-form-urlencoded string using a specific java.nio.charset.Charset Charset .

Usage

From source file:com.qwazr.utils.LinkUtils.java

public final static String UTF8_URL_QuietDecode(String s) {
    try {/*from w  w w.  ja v  a2s .  c o  m*/
        return URLDecoder.decode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return s;
    }
}

From source file:org.pentaho.di.core.util.HttpClientUtil.java

/**
 *
 * @param response the httpresponse for processing
 * @param charset the charset used for getting HttpEntity
 * @param decode determines if the result should be decoded or not
 * @return HttpEntity in String representation using provided charset
 * @throws IOException/*from w w w .j a va  2  s.c  om*/
 */
public static String responseToString(HttpResponse response, Charset charset, boolean decode)
        throws IOException {
    HttpEntity entity = response.getEntity();
    String result = EntityUtils.toString(entity, charset);
    EntityUtils.consume(entity);
    if (decode) {
        result = URLDecoder.decode(result, StandardCharsets.UTF_8.name());
    }
    return result;
}

From source file:com.autentia.intra.servlet.DocServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());
        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getUploadPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }/*from  ww w .j av a  2 s  . c  o  m*/
            OutputStream out = response.getOutputStream();
            InputStream in = new FileInputStream(f);
            byte[] buffer = new byte[8192];
            int nr;
            while ((nr = in.read(buffer)) != -1) {
                out.write(buffer, 0, nr);
            }
            in.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:cn.hxh.springside.utils.EncodeUtils.java

/**
 * URL ?, EncodeUTF-8. /*from   www .ja v a 2 s .c o  m*/
 */
public static String urlDecode(String part) {

    try {
        return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.autentia.intra.servlet.DocRootServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());

        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getDocumentRootPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }/* www . j  a  v  a  2s  .c o  m*/
            OutputStream out = response.getOutputStream();
            InputStream in = new FileInputStream(f);
            byte[] buffer = new byte[8192];
            int nr;
            while ((nr = in.read(buffer)) != -1) {
                out.write(buffer, 0, nr);
            }
            in.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:com.freedomotic.plugin.purl.Purl.java

@Override
protected void onRun() {
    //called in a loop while this plugin is running
    //loops waittime is specified using setPollingWait()
    Authenticator.setDefault(new MyAuthenticator());
    String pageContent = "";
    String url = "";
    try {/*ww  w.  j  a  v  a2s  . c  o m*/
        url = URLDecoder.decode(configuration.getStringProperty("url", ""), "UTF-8");
        pageContent = readPage(new URL(url));
    } catch (Exception ex) {
        Logger.getLogger(Purl.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (!pageContent.isEmpty()) {
        GenericEvent event = new GenericEvent(this);
        event.setDestination("app.event.sensor.url");
        event.addProperty("url", url);
        event.addProperty("url.content", pageContent);
        event.addProperty("url.content.length", String.valueOf(pageContent.length()));
        notifyEvent(event);
        setDescription("Readed " + pageContent.length() + "characters");
    }
}

From source file:com.myb.portal.util.Encodes.java

/**
 * URL ?, EncodeUTF-8. //w  ww  .j a  va 2 s  . c  o  m
 */
public static String urlDecode(String part) {

    try {
        return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        //         throw Exceptions.unchecked(e);
    }
    return part;
}

From source file:com.idea.quickstart.common.security.Encodes.java

/**
 * URL ?, EncodeUTF-8./*  ww  w  .  j ava2  s . c om*/
 */
public static String urlDecode(String part) {
    try {
        return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
    } catch (UnsupportedEncodingException e) {
        //ignore
    }
    return "";
}

From source file:com.liferay.util.Http.java

public static String decodeURL(String url) {
    try {/*from w w w .  j a  v  a 2  s  . c o m*/
        return URLDecoder.decode(url, SystemProperties.get(FILE_ENCODING));
    } catch (UnsupportedEncodingException uee) {
        Logger.error(Http.class, uee.getMessage(), uee);

        return StringPool.BLANK;
    }
}

From source file:com.mashape.unirest.android.http.HttpClientHelper.java

private static HttpRequestBase prepareRequest(HttpRequest request, boolean async) {

    Object defaultHeaders = Options.getOption(Option.DEFAULT_HEADERS);
    if (defaultHeaders != null) {
        @SuppressWarnings("unchecked")
        Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet();
        for (Entry<String, String> entry : entrySet) {
            request.header(entry.getKey(), entry.getValue());
        }//from w  w w . j  av  a2s  .c o m
    }

    if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) {
        request.header(USER_AGENT_HEADER, USER_AGENT);
    }
    if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) {
        request.header(ACCEPT_ENCODING_HEADER, "gzip");
    }

    HttpRequestBase reqObj = null;

    String urlToRequest = null;
    try {
        URL url = new URL(request.getUrl());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
                URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef());
        urlToRequest = uri.toURL().toString();
        if (url.getQuery() != null && !url.getQuery().trim().equals("")) {
            if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
                urlToRequest += "?";
            }
            urlToRequest += url.getQuery();
        } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
            urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    switch (request.getHttpMethod()) {
    case GET:
        reqObj = new HttpGet(urlToRequest);
        break;
    case POST:
        reqObj = new HttpPost(urlToRequest);
        break;
    case PUT:
        reqObj = new HttpPut(urlToRequest);
        break;
    case DELETE:
        //reqObj = new HttpDeleteWithBody(urlToRequest);
        break;
    case PATCH:
        //reqObj = new HttpPatchWithBody(urlToRequest);
        break;
    case OPTIONS:
        reqObj = new HttpOptions(urlToRequest);
        break;
    case HEAD:
        reqObj = new HttpHead(urlToRequest);
        break;
    }

    Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet();
    for (Entry<String, List<String>> entry : entrySet) {
        List<String> values = entry.getValue();
        if (values != null) {
            for (String value : values) {
                reqObj.addHeader(entry.getKey(), value);
            }
        }
    }

    // Set body
    if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) {
        if (request.getBody() != null) {
            HttpEntity entity = request.getBody().getEntity();
            if (async) {
                if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) {
                    reqObj.setHeader(entity.getContentType());
                }
                try {
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    entity.writeTo(output);
                    NByteArrayEntity en = new NByteArrayEntity(output.toByteArray());
                    ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity);
            }
        }
    }

    return reqObj;
}