diff --git a/src/main/java/com/ntlab/irisserver/resources/DrawingsRest.java b/src/main/java/com/ntlab/irisserver/resources/DrawingsRest.java index 083ee2f..13190ca 100644 --- a/src/main/java/com/ntlab/irisserver/resources/DrawingsRest.java +++ b/src/main/java/com/ntlab/irisserver/resources/DrawingsRest.java @@ -1,24 +1,26 @@ package com.ntlab.irisserver.resources; import com.ntlab.irisserver.utils.Base64Decode; -import org.apache.catalina.core.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.beans.BeansException; import org.springframework.stereotype.Component; import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; import java.io.IOException; @Component @Path("/rooms") -public class DrawingsRest { - private ApplicationContext applicationContext; +public class DrawingsRest implements ApplicationContextAware { - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + private org.springframework.context.ApplicationContext applicationContext; + + public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } + // お絵描きの終了判定 @GET @Path("/{rid}/game/drawings") public String getDrawings(@PathParam("rid") String rid) { @@ -26,34 +28,50 @@ return null; } + // 絵のデータ取得(パスを渡せば良い) @GET @Path("/{rid}/game/drawings/{dno}") + @Produces(MediaType.APPLICATION_JSON) public String getDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno ) { - return null; - } - @PUT - @Path("/{rid}/game/drawings/{dno}") - public String putDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno, @FormParam("drawing") String drawing ) { - - String path1 = ""; String path = ""; + String path1 = ""; + String fileName = ""; try { - path1 = applicationContext.getResource("file:").getFile(); - // 下記が本物だけど上手くいかない。。。 - //path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() +"/apache-tomcat-9.0.10"+ "/webapps"; - System.out.println("0"+ path1.toString()); - System.out.println("!!!クライアントから届いたBASE64の文字列!!!:" + drawing.toString()); - path = Base64Decode.saveAsFile(rid, dno, path1, drawing); - + path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps/irisdata"; + fileName= rid + "-" + dno + ".png"; + path = path1 + fileName; + System.out.println("生成されたパス:" + path.toString()); } catch (IOException e) { e.printStackTrace(); throw new WebApplicationException(500); } - // いったんnullにしとく - return null; + return path; + } + + // 描いた絵を送る + @PUT + @Path("/{rid}/game/drawings/{dno}") + @Produces(MediaType.APPLICATION_JSON) + public String putDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno, @FormParam("drawing") String drawing ) { + String path1 = ""; + String path = ""; + + try { + path1 = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps"; + System.out.println("保存前のディレクトリのパス:" + path1.toString()); + System.out.println("クライアントから届いたBASE64の文字列"+ drawing.toString()); + // utilsのBase64Decodeにデコードしてもらって、そのついでに保存してもらう + path = Base64Decode.saveAsFile(rid, dno, path1, drawing); + } catch (IOException e) { + e.printStackTrace(); + throw new WebApplicationException(500); + } + System.out.println("画像デーをが追加しました。これがパス:" + path); + + return "画像保存完了"; } }