2017-03-03 3 views
0

내가 자바 스칼라에서 다음 코드를 변환하려고 만들 :Akka-HTTP 자바 청크 엔티티 (스칼라 변환)

object ChunkedStaticResponse { 
     private def createStaticSource(fileName : String) = 
      FileIO 
      .fromPath(Paths get fileName) 
      .map(p => ChunkStreamPart.apply(p)) 


     private def createChunkedSource(fileName : String) = 
      Chunked(ContentTypes.`text/html(UTF-8)`, createStaticSource(fileName)) 

     def staticResponse(page:String) = 
     HttpResponse(status = StatusCodes.NotFound, 
      entity = createChunkedSource(page)) 
    } 

하지만 두 번째 방법의 구현에 문제가 있어요. 지금까지 내가 멀리있어 :

class ChunkedStaticResponseJ { 

    private Source<HttpEntity.ChunkStreamPart, CompletionStage<IOResult>> 
    createStaticSource(String fileName) { 
    return FileIO 
      .fromPath(Paths.get(fileName)) 
      .map(p -> HttpEntity.ChunkStreamPart.create(p)); 
    } 

    private HttpEntity.Chunked createChunkedSource(String fileName) { 
    return HttpEntities.create(ContentTypes.TEXT_HTML_UTF8, 
      createStaticSource(fileName)); // not working 
    } 

    public HttpResponse staticResponse(String page) { 
    HttpResponse resp = HttpResponse.create(); 
    return resp.withStatus(StatusCodes.NOT_FOUND).withEntity(createChunkedSource(page)); 
    } 
} 

두 번째 방법으로 청크 소스를 만드는 방법을 알아낼 수 없습니다. 누군가가 접근법을 제안 할 수 있습니까? 또한 나는 일반적으로 올바른 길을 가고 있습니까? 당신이 당신의 파일에서 읽은 모든 ByteString 요소의 밖으로 Chunk을 만들려면

답변

1

, 당신은 Chunked.fromData합니다 (JavaDSL에서 HttpEntities.createChunked)를 활용할 수 있습니다. 여기

는 결과가 스칼라 측

object ChunkedStaticResponse { 
    private def createChunkedSource(fileName : String) = 
     Chunked.fromData(ContentTypes.`text/html(UTF-8)`, FileIO.fromPath(Paths get fileName)) 

    def staticResponse(page:String) = 
     HttpResponse(status = StatusCodes.NotFound, 
     entity = createChunkedSource(page)) 
    } 

에 보일 것이이 JavaDSL 대응 될 방법

class ChunkedStaticResponseJ { 
    private HttpEntity.Chunked createChunkedSource(String fileName) { 
     return HttpEntities.createChunked(ContentTypes.TEXT_HTML_UTF8, FileIO.fromPath(Paths.get(fileName))); 
    } 

    public HttpResponse staticResponse(String page) { 
     HttpResponse resp = HttpResponse.create(); 
     return resp.withStatus(StatusCodes.NOT_FOUND).withEntity(createChunkedSource(page)); 
    } 
}