14 Aralık 2022 Çarşamba

Apache Commons Exec

Giriş
Bir shell script veya başar bir uygulamayı çalıştırıp çıktısını okumak için kullanılır
DefaultExecutor harici uygulamayı çalıştırır
PumpStreamHandler çıktıyı toparlar

Gradle
Şu satırı dahil ederiz
implementation group: 'org.apache.commons', name: 'commons-exec', version: '1.3'

5 Aralık 2022 Pazartesi

HttpComponents HttpClientBuilder Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.http.impl.client.HttpClientBuilder;
setDefaultRequestConfig metodu
Tüm client için timeout ayarları yapılabilir.
Örnek
Şöyle yaparız
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

HttpGet request = new HttpGet("https://httpbin.org/get");

// 5 seconds timeout
RequestConfig requestConfig = RequestConfig.custom()
  .setConnectionRequestTimeout(5_000)
  .setConnectTimeout(5_000)
  .setSocketTimeout(5_000)
  .build();

try (CloseableHttpClient httpClient = HttpClientBuilder.create()
  .setDefaultRequestConfig(requestConfig)
  .build();
   CloseableHttpResponse response = httpClient.execute(request)) {
  ...
}
setUserAgent metodu
Şöyle yaparız.
CloseableHttpClient httpClient = HttpClients.custom()
  .setUserAgent(HTTP_USER_AGENT).
  .build();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
setSSLSocketFactory metodu
Örnek
Şöyle yaparız
HttpClientBuilder builder = HttpClients.custom();
javax.net.ssl.SSLContext sslContext = SSLContext.getInstance("TLSv1.1");
javax.net.ssl.KeyManager[] clientKeyManagers = ...;
javax.net.ssl.TrustManager[] clientTrustManagers = ...;
sslContext.init(clientKeyManagers, clientTrustManagers, new SecureRandom());

builder.setSSLSocketFactory(
  new org.apache.http.conn.ssl.SSLConnectionSocketFactory(sslContext,
    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
);
CloseableHttpClient client = builder.build();

27 Ekim 2022 Perşembe

Failable Sınıfı - Lambda İçinde Exception İçindir

Giriş
Şu satırı dahil ederiz
import org.apache.commons.lang3.function.Failable;
Örnek
Şöyle yaparız
Stream<String> stream = Stream.of("...", "...", "...");
Failable.stream(stream)
  //throws a ClassNotFoundException wrapped in an UndeclaredThrowableException at runtime. 
  .map(Class::forName)  
  .forEach(System.out::println);



23 Eylül 2022 Cuma

Camel Choice ve When - Content Based Routing İçindir

Giriş
Açıklaması şöyle
Apache Camel contains a powerful feature called content-based routers. This allows you to process the message differently relying on the content.

They are quite similar to the if/else statement in Java. Regardless, in Camel, the equivalent words are when and otherwise.
Örnek
Şöyle yaparız
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ChoiceWhenRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {

    from("direct:startWhenChoiceRoute")
      .routeId("direct:startWhenChoiceRoute")
      .log( "${body}")
      .choice()
      .when(body().isNull())
      .log("Message body is empty.")
      .log("${body}")
      .end();
  }
}
Test için şöyle yaparız
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;

@RestController
public class Controler {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private CamelContext camelContext;

  @GetMapping
  public void function() {
    Exchange requestExchange = ExchangeBuilder
      .anExchange(camelContext)
      .build();

    producerTemplate
      .send("direct:startWhenChoiceRoute",requestExchange);

  }
}



18 Nisan 2022 Pazartesi

Jena - Semantic Web

Gradle
Şu satırı dahil ederiz
implementation "org.apache.jena:apache-jena-libs:4.0.0"
Örnek
Şöyle yaparız
OntModel model = ModelFactory.createOntologyModel("http://www.w3.org/2000/01/rdf-schema#");
RDFParser.source("https://schema.org/version/latest/schemaorg-current-https.jsonld")
  .parse(model);