<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Küstermann</title>
    <description>The latest articles on DEV Community by Thomas Küstermann (@thokuest).</description>
    <link>https://dev.to/thokuest</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F7240%2F1uDyrAgA.jpg</url>
      <title>DEV Community: Thomas Küstermann</title>
      <link>https://dev.to/thokuest</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thokuest"/>
    <language>en</language>
    <item>
      <title>Is it okay to use com.sun.net.httpserver.HttpServer?</title>
      <dc:creator>Thomas Küstermann</dc:creator>
      <pubDate>Sat, 12 Nov 2022 15:29:50 +0000</pubDate>
      <link>https://dev.to/thokuest/is-it-okay-to-use-comsunnethttpserverhttpserver-24il</link>
      <guid>https://dev.to/thokuest/is-it-okay-to-use-comsunnethttpserverhttpserver-24il</guid>
      <description>&lt;p&gt;I've been recently working on a simple CRUD microservice implemented in Java. You may immediately think of using frameworks such as &lt;a href="https://spring.io/projects/spring-boot"&gt;Spring Boot&lt;/a&gt;, &lt;a href="https://quarkus.io/"&gt;Quarkus&lt;/a&gt; or &lt;a href="https://micronaut.io/"&gt;Micronaunt&lt;/a&gt;. However, no frameworks were allowed, and libraries were only permitted, if the functionality provided is not natively available or too complex to build. Long story short, I had to find a HTTP server implementation.&lt;/p&gt;

&lt;p&gt;After searching around a bit, I eventually found &lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/jdk.httpserver/com/sun/net/httpserver/HttpServer.html"&gt;com.sun.net.httpserver.HttpServer&lt;/a&gt; readily available. It's pretty much low level but apart from that straightforward to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldServer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"It works!"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;HttpServer&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpServer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InetSocketAddress&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StandardCharsets&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;UTF_8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="nc"&gt;Headers&lt;/span&gt; &lt;span class="n"&gt;responseHeaders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResponseHeaders&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;responseHeaders&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"text/plain; charset=UTF-8"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sendResponseHeaders&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="nc"&gt;OutputStream&lt;/span&gt; &lt;span class="n"&gt;responseBodyStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getResponseBody&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;responseBodyStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;responseBodyStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;responseBodyStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;

        &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait a sec, it's from package &lt;code&gt;com.sun&lt;/code&gt; and we were told it's not correct to use it, right? You may remember &lt;a href="https://www.oracle.com/java/technologies/faq-sun-packages.html"&gt;Why Developers Should Not Write Programs That Call 'sun' Packages&lt;/a&gt;. On the other hand, it talks about &lt;code&gt;sun&lt;/code&gt; packages and not &lt;code&gt;com.sun&lt;/code&gt; - confusing!&lt;/p&gt;

&lt;p&gt;So, is it actually safe to use &lt;code&gt;com.sun.net.httpserver.HttpServer&lt;/code&gt;? Luckily, it is, and the answer is finally given in &lt;a href="https://openjdk.org/jeps/403"&gt;JEP 403: Strongly Encapsulate JDK Internals&lt;/a&gt; targeting Java 17.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Exported com.sun APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most &lt;code&gt;com.sun.*&lt;/code&gt; packages in the JDK are for internal use, but a few are supported for external use. These supported packages were exported in JDK 9 and will continue to be exported, so you can continue to program against their public APIs. They will, however, no longer be open. Examples include&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Compiler Tree API in the &lt;code&gt;jdk.compiler&lt;/code&gt; module,&lt;/li&gt;
&lt;li&gt;
The HTTP Server API in the &lt;code&gt;jdk.httpserver&lt;/code&gt; module,&lt;/li&gt;
&lt;li&gt;The SCTP API in the &lt;code&gt;jdk.sctp&lt;/code&gt; module, and&lt;/li&gt;
&lt;li&gt;JDK-specific extensions to the NIO API in the &lt;code&gt;com.sun.nio.file package&lt;/code&gt; of the &lt;code&gt;jdk.unsupported&lt;/code&gt; module.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  SonarLint and java:S1191
&lt;/h2&gt;

&lt;p&gt;SonarLint (and SonarQube) issuea a major code smell warning when code from package &lt;code&gt;com.sun&lt;/code&gt; is used (&lt;a href="https://sonarsource.github.io/rspec/#/rspec/S1191"&gt;java:S1191&lt;/a&gt;):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Classes from "sun.*" packages should not be used&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Classes in the &lt;code&gt;sun.*&lt;/code&gt; packages are considered implementation details, and are not part of the Java API.&lt;/p&gt;

&lt;p&gt;They can cause problems when moving to new versions of Java because there is no backwards compatibility guarantee. Similarly, they can cause problems when moving to a different Java vendor, such as OpenJDK.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Luckily, a ticket has been filed to relax this rule: &lt;a href="https://sonarsource.atlassian.net/browse/SONARJAVA-4382"&gt;SONARJAVA-4382&lt;/a&gt; (S1191 should not raise issues on imports from &lt;code&gt;com.sun.*&lt;/code&gt; packages).&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Downloading Files With Groovy and Authentication</title>
      <dc:creator>Thomas Küstermann</dc:creator>
      <pubDate>Fri, 30 Aug 2019 12:38:49 +0000</pubDate>
      <link>https://dev.to/thokuest/downloading-files-with-groovy-and-authentication-6bb</link>
      <guid>https://dev.to/thokuest/downloading-files-with-groovy-and-authentication-6bb</guid>
      <description>&lt;p&gt;Downloading a file from a URL requiring user authentication with Groovy is as easy as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;net&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Authenticator&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setDefault&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authenticator&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;PasswordAuthentication&lt;/span&gt; &lt;span class="nf"&gt;getPasswordAuthentication&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PasswordAuthentication&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toCharArray&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;destinationFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withOutputStream&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;downloadUrl&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;newInputStream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Links&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stackoverflow: &lt;a href="https://stackoverflow.com/questions/50062880/groovy-download-file-with-authentication"&gt;groovy - Download file with authentication&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Stackoverflow: &lt;a href="https://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java/496707#496707"&gt;Connecting to remote URL which requires authentication using Java&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>groovy</category>
      <category>snippet</category>
    </item>
  </channel>
</rss>
