2017-03-03 13 views
0

(スカラ座からの変換)Javaでチャンクされたエンティティを作成し、私は、JavaへのScalaから次のコードを変換しようとしています:アッカ-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)を活用することができます。ここで

は結果がScalaの側

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)); 
    } 
} 
関連する問題