28 Temmuz 2020 Salı

POI Excel OPCPackage Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.poi.openxml4j.opc.OPCPackage
XSSFReader nesnesi yaratmak için kullanılır.

open metodu - InputStream
Örnek - XSSFWorkbook 
Şöyle yaparız
import org.apache.poi.ss.usermodel.*;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

File file = new File("./inspection.xlsx");

//OPCPackage pkg = OPCPackage.open(file);
OPCPackage pkg = OPCPackage.open(new FileInputStream(file));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
    
//XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
//Workbook wb = WorkbookFactory.create(new FileInputStream(file));

//wb -> sheets -> rows -> cols
int finding = 445;

DataFormatter formatter = new DataFormatter();
boolean write = false;
for(Sheet sheet : wb) {
  for(Row row : sheet) {
    if(row.getCell(0)!=null && !formatter.formatCellValue(row.getCell(0)).equals("")) {
      Cell cell = row.getCell(0);
      String text = formatter.formatCellValue(cell);
      if('0'<=text.charAt(0) && text.charAt(0)<='9') {
        int id = Integer.parseInt(text);
        if (id == finding) {
          System.out.println(sheet.getSheetName());
          System.out.println(sheet.getRow(row.getRowNum()).getCell(1));
          Cell cellCurrent = row.getCell(2);
          if (cellCurrent == null) {
            cellCurrent = row.createCell(2);
          }
          cellCurrent.setCellValue("X");
          write = true;
        }
      }
    }
  }
}
if (write) {
  FileOutputStream outputStream = new FileOutputStream(file);
  wb.write(outputStream);
  outputStream.close();
  wb.close();
}
Örnek - XSSReader
Şöyle yaparız
def pkg = OPCPackage.open(request.getBody(InputStream.class))
def xssfReader = new XSSFReader(pkg)

27 Temmuz 2020 Pazartesi

Camel JettyComponent Sınıfı

from metodu
Örnek
Şöyle yaparız
from("jetty://0.0.0.0:8080/searchProduct").routeId("searchProduct.products").threads()
.log(LoggingLevel.INFO, "searchProducts request Received with body: ${body}")
.bean(Processor.class, "createSearchProductsRequest").removeHeaders("CamelHttp*")
.setHeader(Exchange.HTTP_METHOD,
    constant(org.apache.camel.component.http4.HttpMethods.POST))
.to("http4://" + preLiveBaseAPI + searchProductsUrl + "?apiKey=" + ApiKey
    + "&bridgeEndpoint=true")
.bean(Processor.class, "buildResponse")
.log(LoggingLevel.INFO, "Search products finished");
get metodu
Örnek - jetty
Şu satırı dahil ederiz
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-jetty-starter</artifactId>
  </dependency>
  ..
  ..<!-- all other required dependencies are in place-->
  ..
</dependencies>
Şöyle yaparız
restConfiguration()
  .component("jetty")
  .port("8080");
        
rest()
  .path("/api")
  .get("/{name}")
   ..
   ..
  .to("seda:greeting");
port metodu
Şöyle yaparız
@Component
public class EmployeePackageRoute extends RouteBuilder {
        
  @Override
  public void configure() {
    try{
      restConfiguration().component("jetty").port(8000).bindingMode(RestBindingMode.json);
      rest("/api/test/getTestResponse")
        .get().consumes(MediaType.APPLICATION_JSON_VALUE).route().setBody()
.constant("return test response"); rest("/api/bcr/recurringPaymentsDeduct") .post().consumes(MediaType.APPLICATION_JSON_VALUE)
.type(RecurringPaymentResults.class).outType(ResponseEntity.class).route() .process(ex -> { ... ex.getOut().setBody(employee); }) .to("seda:recurringPaymentsFeed"); }catch(Exception e) { ... } } }
unmarshal metodu
Şöyle yaparız.
from("jetty:http://0.0.0.0:8080/api/camel/appoverview")
.to("http4://10.150.60.237:80/api/itsb/applicationoverview?httpMethod=GET&
  bridgeEndpoint=true&throwExceptionOnFailure=true")
.unmarshal().json(JsonLibrary.Jackson)
.transform().simple("${body}")
.log("Response sent -> ${body}");
Jackson için şöyle yaparız
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jackson</artifactId>
</dependency>

Camel ServletComponent Sınıfı

Giriş
Örneğin veri POST edilebilecek bir URL yaratır

Örnek
Şöyle yaparız
camel:
 springboot:
  name: gateway
 component:
  servlet:
   mapping:
    enabled: true
    context-path: /*
  mail:
   basic-property-binding: true
Örnek
Açıklaması şöyle
... the Java DSL given below will create a REST endpoint which will be able to accept an XML consisting of List of Products and splits it into multiple products and invoke Process method of BrandProcessor with it. And just by adding .parallelProcessing it will parallel process all the Product Objects. (Product class is JAXB/XJC generated Java stub from the XSD which the input xml is confined to.) 
Şöyle yaparız
from("servlet:item-delta?matchOnUriPrefix=true&httpMethodRestrict=POST")
.routeId("Item-DeltaRESTRoute")
.log(LoggingLevel.INFO, "Item Delta received on Item-DeltaRESTRoute")
.split(stax(Product.class))
.parallelProcessing()
.process(itemDeltaProcessor);