Java: Unterschied zwischen den Versionen

Aus wiki
Wechseln zu: Navigation, Suche
K (Not Null)
 
(16 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
= Code Snippets =
 
= Code Snippets =
 +
[[Authentication Methods]]
 +
== WAR-File generieren ==
 +
Aus dem lokalen Verzeichnis. (web.xml in WEB-INF Ordner muss existieren)
 +
jar cvf SecuritySuiteDemo.war .
 +
 +
== Date Pattern ==
 +
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 2001-07-04T12:08:56.235-0700
 +
 
== Regex ==
 
== Regex ==
 
=== Pattern Matching ===
 
=== Pattern Matching ===
Zeile 14: Zeile 22:
 
  }
 
  }
 
  </syntaxhighlight>
 
  </syntaxhighlight>
 +
 +
 +
== Encoding ==
 +
Base64 Encoding (Basic auth)
 +
String base64encodedUsernameAndPassword = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
 +
webClient.addRequestHeader("Authorization", "Basic " + base64encodedUsernameAndPassword);
 +
 +
== URL Parsing ==
 +
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
 +
 +
== HttpsUrlConnection ==
 +
In Case of any ClassCast Exception:
 +
 +
java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl cannot be cast to javax.net.ssl.HttpsURLConnection
 +
HttpsURLConnection con = (HttpsURLConnection) new URL(null,"http://httpbin.org/digest-auth/auth/user/passwd",
 +
new sun.net.www.protocol.https.Handler()).openConnection();
 +
con.connect();
 +
 +
== InputStream Reader ==
 +
        int c;
 +
        StringBuilder responseJsonString = new StringBuilder();
 +
        int len = connection.getContentLength();
 +
        System.out.println("Content-Length: " + len);
 +
        if (len > 0) {
 +
            InputStream input = connection.getInputStream();
 +
            int i = len;
 +
            while (((c = input.read()) != -1) && (-i > 0)) {
 +
                System.out.print((char) c);
 +
                responseJsonString.append((char) c);
 +
            }
 +
            input.close();
 +
        } else {
 +
    }
 +
 +
== Mapping Object to JSON ==
 +
(with Jackson JSON Mapper)
 +
 +
objectMapper = new ObjectMapper();
 +
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
 +
objectMapper.enable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS);
 +
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
 +
objectMapper.setSerializationInclusion(Include.NON_NULL);
 +
String jsonElement = null;
 +
try {
 +
  jsonElement = objectMapper.writeValueAsString(a);
 +
} catch (JsonProcessingException e) {
 +
e.printStackTrace();
 +
}
 +
 +
== Generic Method ==
 +
 +
private <T> T useIncomingIfPossible(T incoming, T existing) {
 +
    return incoming != null ? incoming : existing;
 +
  }
 +
 +
 +
== Not Null ==

Aktuelle Version vom 21. Mai 2019, 18:22 Uhr

Code Snippets

Authentication Methods

WAR-File generieren

Aus dem lokalen Verzeichnis. (web.xml in WEB-INF Ordner muss existieren)

jar cvf SecuritySuiteDemo.war .

Date Pattern

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"	2001-07-04T12:08:56.235-0700

Regex

Pattern Matching

Named Matching

<syntaxhighlight lang="java">
private @Nullable String getToken(@Nonnull String responseURL) {</pre>
String patternString = "http://www.example.com/example#(token%3D(?<TOKEN>.+))%26otherstuff(.+%)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(responseURL);
if(matcher.find()){
return matcher.group("TOKEN");
}
return null;
}
</syntaxhighlight>


Encoding

Base64 Encoding (Basic auth)

String base64encodedUsernameAndPassword = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes());
webClient.addRequestHeader("Authorization", "Basic " + base64encodedUsernameAndPassword);

URL Parsing

List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");

HttpsUrlConnection

In Case of any ClassCast Exception:

java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl cannot be cast to javax.net.ssl.HttpsURLConnection

HttpsURLConnection con = (HttpsURLConnection) new URL(null,"http://httpbin.org/digest-auth/auth/user/passwd",
new sun.net.www.protocol.https.Handler()).openConnection();
con.connect();

InputStream Reader

       int c;
       StringBuilder responseJsonString = new StringBuilder();
       int len = connection.getContentLength();
       System.out.println("Content-Length: " + len);
       if (len > 0) {
           InputStream input = connection.getInputStream();
           int i = len;
           while (((c = input.read()) != -1) && (-i > 0)) {
               System.out.print((char) c);
               responseJsonString.append((char) c);
           }
           input.close();
       } else {
   }

Mapping Object to JSON

(with Jackson JSON Mapper)
objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
objectMapper.enable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setSerializationInclusion(Include.NON_NULL);
String jsonElement = null;
try {
 jsonElement = objectMapper.writeValueAsString(a);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

Generic Method

private <T> T useIncomingIfPossible(T incoming, T existing) {
   return incoming != null ? incoming : existing;
 }


Not Null