0

I have a Dynamic Web Project + Maven deployed on WebSphere 8.5 with java 1.6 and I'm using the following library in the pom.xml:

<dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
    </dependency>

I get the environment's url and I'm trying to write it on a .json file. Here is the code:

InetAddress ip;
    try {
        ip = InetAddress.getLocalHost();
        ip.getHostAddress();

        JSONObject obj = new JSONObject();
        obj.put("name", "https:" + File.separatorChar + File.separatorChar + ip.getHostAddress() + ":443"
                + File.separatorChar + "authorizer" + File.separatorChar);
        obj.put("debugging", "true");

        FileWriter file = new FileWriter(ctx.getServletContext().getRealPath("/") + "/assets/conf.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

Well it works, but I get this on conf.json:

{"name":"https:\/\/126.0.0.0:443\/authorizer\/","debugging":"true"}

As you see, it is not formatted but when I have tested it on Windows just worked fine. The thing is when I deployed on WebSphere I got the url wrong and the problem It's on the way of the forward slash. How can I write the url correctly?

I have tried already with File.separator, or simply writing "/" or "\\" but neither worked.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Since you are trying to construct a URL, and not a path to a file in the file system, you should not be using `File.separator`, you should simply use "/". On Windows OSs File.separator is a \, on unix-based OSs, it is /. On Windows particularly, you don't want the URLS to _ever_ have \. – StvnBrkdll Apr 15 '18 at 18:00
  • I got this {"name":"https:\/126.0.0.0:443\/authorizer\/","debugging":"true"} – D. Garcés Apr 15 '18 at 18:18

1 Answers1

0

Don't use File.separator to construct URLs, simply use /. The path-separator for URLs is the same regardless of the OS. File.separator is different depending on the OS (Windows is \, most other OSs are /). I rewrote a portion of your code to demo the construction of the URL. Notice when you get the URL from the Json object, it is formatted properly (see the line: System.out.println("the URL recovered from Json:...). The output from that line looks like:

the URL recovered from Json: https://127.0.1.1:443/authorizer/

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

    InetAddress ip;
    ip = InetAddress.getLocalHost();
    ip.getHostAddress();

    String url0 = "https:" + File.separatorChar + File.separatorChar + ip.getHostAddress() + ":443"
            + File.separatorChar + "authorizer" + File.separatorChar;
    String url1 = "https:" + "//" + ip.getHostAddress() + ":443"
            + "/" + "authorizer" + "/";
    JSONObject obj = new JSONObject();
    obj.put("name", url0);
    obj.put("debugging", "true");

    System.out.println("the URL recovered from Json: " + obj.get("name"));

    System.out.println("json: " + obj.toJSONString());
    System.out.println("this url will be different than what you want on Windows:\n" + url0);
    System.out.println("this is what you want, it will be the same on all OSs:\n" + url1);
}

It appears that Json allows / to be escaped, but does not require it. So escaping the / is not incorrect, just unnecessary in your case. See: JSON: why are forward slashes escaped?

StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31