Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any method to get constant for HTTP and HTTPS values

Tags:

java

rest

spring

Is there a class whene HTTP and HTTPs constant value is defined. As of now I have created a custom class.

   public static class UrlConstants {

    private UrlConstants() {
        super();
    }

    /** The Constant HTTP. */
    public static final String HTTP = "http";
}
like image 763
Bhawna Joshi Avatar asked Mar 30 '26 14:03

Bhawna Joshi


2 Answers

As far as I can tell (as of JDK 8 which I have handy), the best you can do within the Java JDK is

javax.print.attribute.standard.ReferenceUriSchemesSupported

which provides a number of URI-Schemes:

import javax.print.attribute.standard.ReferenceUriSchemesSupported;
// ...
System.out.println(ReferenceUriSchemesSupported.HTTPS); // => "https"

Of course, it also has GOPHER, NNTP, NEWS, and a few other out-of-date schemes, but it does provide what you want.

Other answers on the web suggest other frameworks, many of which do define the two protocols.

Otherwise, you just need something like this:

package demo;

public enum Scheme {
    HTTP("http"),
    HTTPS("https");
    private final String scheme;

    private Scheme(String scheme) {
        this.scheme = scheme;
    }

    @Override
    public String toString() {
        return scheme;
    }
}
like image 151
luskwater Avatar answered Apr 02 '26 03:04

luskwater


A little clunky, but these work.

String.valueOf(org.apache.commons.httpclient.HttpURL.DEFAULT_SCHEME) String.valueOf(org.apache.commons.httpclient.HttpsURL.DEFAULT_SCHEME)

There's also org.mortbay.jetty.HttpSchemes.HTTP and org.mortbay.jetty.HttpSchemes.HTTPS.

like image 22
An̲̳̳drew Avatar answered Apr 02 '26 02:04

An̲̳̳drew