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.reversemind.glia.test.go3.java

public static void main(String... args) throws Exception {

    System.setProperty("http.proxyHost", "10.105.0.217");
    System.setProperty("http.proxyPort", "3128");

    System.setProperty("https.proxyHost", "10.105.0.217");
    System.setProperty("https.proxyPort", "3128");

    HttpHost proxy = new HttpHost("10.105.0.217", 3128);
    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    HttpGet request = new HttpGet(
            "https://twitter.com/i/profiles/show/splix/timeline/with_replies?include_available_features=1&include_entities=1&max_id=285605679744569344");
    HttpResponse response = client.execute(request);

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

    String sl = "";
    String line = "";
    while ((line = rd.readLine()) != null) {
        LOG.debug(line);/*www  . jav a  2s.  co m*/
        sl += line;
    }

    sl = new String(sl.getBytes(), "UTF-8");
    String sss = sl.replaceAll("\\{1,}", "\\").replaceAll("\\\"", "'").replaceAll("\\"", "'")
            .replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&amp;", "&").replaceAll("&apos;", "'")
            .replaceAll("\u003c", "<").replaceAll("\u003e", ">").replaceAll("\n", " ").replaceAll("\\/", "/")
            .replaceAll("\\'", "'");

    String sss2 = sss.replaceAll("\\'", "'");
    LOG.debug(sss);

    save("/opt/_del/go_sl.txt", sl);
    save("/opt/_del/go_sss.txt", sss);
    save("/opt/_del/go_line.txt", line);
    save("/opt/_del/go_sss2.txt", sss2);

    LOG.debug("\n\n\n\n\n");
    LOG.debug(sss);
    LOG.debug("\n\n\n\n\n");
    LOG.debug(URLDecoder.decode(sl, "UTF-8"));
    LOG.debug(URLDecoder.decode("\u0438\u043d\u043e\u0433\u0434\u0430", "UTF-8"));

    LOG.debug(URLDecoder.decode("\n            \u003c/span\u003e\n            \u003cb\u003e\n ", "UTF-8"));

}

From source file:com.gemini.geminimain.GeminiMain.java

public static void main(String[] args) throws IOException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    //create table for the application, networks and servers
    ds = morphia.createDatastore(mongoClient, "Gemini");

    //create the mapper
    Injector injector = Guice.createInjector(new GeminiMapperModule());
    GeminiMapper mapper = injector.getInstance(GeminiMapper.class);

    //set the current logging level to debug
    Configurator.currentConfig().level(Level.INFO).activate();

    //create some data to transfer to the front end
    createSampleData();//from  www  .  ja v a 2s. c  o  m

    //close the db client
    mongoClient.close();

    //check if authenticated, create the call context and user context here
    //for now it is empty!!!!
    before((request, response) -> {
        boolean authenticated = true;
        // ... check if authenticated
        if (!authenticated) {
            halt(401, "Nice try, you are not welcome here");
        }
    });

    after((request, response) -> {
        response.header("Access-Control-Allow-Origin", "*");
        //response.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        //response.header("Access-Control-Max-Age", "3600");
        //response.header("Access-Control-Allow-Headers", "x-requested-with");
    });

    //get all environments of the tenant
    get("/environments/:tenantid", "application/json", (request, response) -> {
        String tenantID = request.params("tenantid");
        try {
            List<GeminiEnvironment> lEnvs = getEnvironments(tenantID);
            if (lEnvs != null) {
                response.status(200);
                Logger.info("Found environments for tenant {}", tenantID);
                List<GeminiEnvironmentDTO> dtoEnvs = new ArrayList();
                lEnvs.stream().forEach(e -> dtoEnvs.add(mapper.getDTOFromEnv(e)));
                return lEnvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }

        return "not implemented yet";
    }, new JsonTransformer());

    //return all applications for a given tenant and environment
    get("/applications/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        try {
            List<GeminiApplication> apps = getEnvApplications(tenantID, envName);
            if (apps == null || apps.isEmpty()) {
                response.status(404);
                Logger.info("Could not find any applications.");
                return "No Applications found.";
            } else {
                response.status(200);
                Logger.debug("Found applications");
                List<GeminiApplicationDTO> dtoApps = new ArrayList();
                apps.stream().forEach((a) -> {
                    dtoApps.add(mapper.getDTOFromApp(a));
                });
                return dtoApps;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return application given a name
    get("/applications/:name", "application/json", (request, response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - application Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL - server name " + appName;
        }
        try {
            GeminiApplication a = getAppByName(appName);
            if (a != null) {
                Logger.debug("Found application {}", appName);
                return mapper.getDTOFromApp(a);
            } else {
                Logger.info("Could not find application {}", appName);
                return "Could not find application " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/applications", (request, response) -> {
        String body = request.body();
        return "Hello World: " + request.body();
    });

    //return all networks related to application with ID = ':id'
    get("/applications/:name/networks", "application/json", (Request request, Response response) -> {
        String appName;
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> lNet = getAppNetworks(appName);
            if (lNet != null) {
                Logger.debug("Found networks for application {}", appName);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                lNet.stream().forEach(aNet -> dtoNets.add(mapper.getDTOFromNetwork(aNet)));
                return dtoNets;
            } else {
                response.status(404);
                Logger.info("Could not find any networks for application {}", appName);
                return "Could not find any networks for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':id'
    get("/applications/:id/servers", "application/json", (Request request, Response response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getAppServers(appName);
            if (lSrv != null) {
                Logger.debug("Found servers for application {}", appName);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            } else {
                Logger.info("Could not find servers for application {}", appName);
                response.status(404);
                return "Could not find servers for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':appID' AND network with ID = ':nID'
    get("/applications/:appname/networks/:netstart/:netend/servers", "application/json",
            (request, response) -> {
                String appName = "", netStart = "", netEnd = "";
                //decode the URL as it may contain escape characters, etc.
                try {
                    appName = URLDecoder.decode(request.params(":appname"), "UTF-8");
                    netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
                    netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                    Logger.error(
                            "Severe Error: Unsupported encoding in URL - application {} with network start: {} and end: {} Exception {}",
                            request.params(":appname"), request.params(":netstart"), request.params(":netend"),
                            ex);
                    return "Severe Error: Unsupported encoding in URL";
                }

                //get the servers for app network
                try {
                    List<GeminiServer> lSrv = getAppNetworkServers(appName, netStart, netEnd);
                    if (lSrv == null || lSrv.isEmpty()) {
                        Logger.info("No servers for application {} with network start: {} and end: {}", appName,
                                netStart, netEnd);
                        response.status(404);
                        return "No servers for application " + appName + " with network start: " + netStart
                                + " and end: " + netEnd;
                    } else {
                        Logger.debug("Found servers for application {} with network start: {} and end: ",
                                appName, netStart, netEnd);
                        List<GeminiServerDTO> dtoSrvs = new ArrayList();
                        for (GeminiServer s : lSrv) {
                            dtoSrvs.add(mapper.getDTOFromServer(s));
                        }
                        return dtoSrvs;
                    }
                } catch (UnknownHostException ex) {
                    Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
                    return "Severe Error: Unknown database host " + DB_SERVER;
                }
            }, new JsonTransformer());

    //get the networks for a tenant and environment,
    get("/networks/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> nets = getEnvNetworks(tenantID, envName);
            if (nets == null) {
                Logger.info("No networks discovered for tenant {} in environment {}", tenantID, envName);
                return "No networks discovered for tenant" + tenantID + "in environment" + envName;
            } else {
                response.status(200);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                nets.stream().forEach(n -> dtoNets.add(mapper.getDTOFromNetwork(n)));
                Logger.debug("Found {} networks for tenant {} env {}", nets.size(), tenantID, envName);
                return dtoNets;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown datbase host - {}", DB_SERVER);
            return "Severe Error: Unknown data host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiNetwork n = getNetworkFromDB(netStart, netEnd);
            if (n == null) {
                Logger.info("No network with start {} and end {} found", netStart, netEnd);
                return "No network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found network with start {} and end {} ", netStart, netEnd);
                return mapper.getDTOFromNetwork(n);
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend/servers", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getNetworkServersFromDB(netStart, netEnd);
            if (lSrv == null) {
                Logger.info("No servers in network with start {} and end {} found", netStart, netEnd);
                return "No servers in network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found servers in network with start {} and end {} ", netStart, netEnd);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        //return the discovered networks
        //            DiscoverNetworkRange newNet = new DiscoverNetworkRange(netStart, netEnd);
        //            List<GeminiNetwork> lNet;
        //            if (autoDiscover) {
        //                try {
        //                    //start discovering...
        //                    lNet = discoverNetworks(netStart, netEnd);
        //                } catch (IOException ex) {
        //                    java.util.logging.Logger.getLogger(GeminiMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        //                }
        //            }

        //since all the services are running on the same computer
        response.header("Access-Control-Allow-Origin", "*");

        //return the networks...
        return "no networks";
    }, new JsonTransformer());

    //get all servers for tenant within an environment
    get("/servers", "application/json", (request, response) -> {
        try {
            List<GeminiServer> srvs = getServersFromDB();
            if (srvs == null) {
                Logger.info("Found no servers in database");
                return "No Networks";
            } else {
                Logger.debug("Found servers in database");
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : srvs) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                response.status(200);
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/servers/:name", "application/json", (request, response) -> {
        String srvName;
        try {
            srvName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - {} Exception {}", request.params(":name"),
                    ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiServer s = getServerFromDB(srvName);
            if (s == null) {
                Logger.info("No server with name {} found", srvName);
                return "No server with name " + srvName;

            } else {
                Logger.debug("Found server with name {}", srvName);
                return mapper.getDTOFromServer(s);
            }

        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

}

From source file:Main.java

public static String getDecodeString(String str) {
    java.net.URLDecoder ud = new URLDecoder();
    try {//ww w.  j ava 2  s . co m
        str = ud.decode(str, "utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static String urlDecode(String s) {
    try {//from   w  w  w .ja v a 2  s  . co  m
        return URLDecoder.decode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return URLDecoder.decode(s);
    }
}

From source file:Main.java

public static String urlDecode(String text) {
    try {//  w w  w . j a  v  a2  s.  c  o  m
        return URLDecoder.decode(text, "UTF-8");
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String urlDecode(String url) {
    try {/*from  w w  w. j a v a  2s.c o  m*/
        url = URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return url;
}

From source file:Main.java

public static String urlDecoder(String code) {
    try {//from  w  w  w.ja  v  a2  s  . c om
        return URLDecoder.decode(code, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static final String decodeURL(String str) {
    try {//from  www  .j  ava 2  s.  c o  m
        return URLDecoder.decode(str, "utf-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String decode(String msg) throws UnsupportedEncodingException {
    return URLDecoder.decode(msg, "UTF-8");
}

From source file:Main.java

public static String urlEncoderToString(String code, String charSet) {
    try {//from w w  w  .j  a  v  a 2s.  c o m
        return URLDecoder.decode(code, charSet);
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}