diff --git a/src/main/java/com/ntlab/irisserver/models/KeywordManager.java b/src/main/java/com/ntlab/irisserver/models/KeywordManager.java index 2103fd6..c175210 100644 --- a/src/main/java/com/ntlab/irisserver/models/KeywordManager.java +++ b/src/main/java/com/ntlab/irisserver/models/KeywordManager.java @@ -1,54 +1,22 @@ package com.ntlab.irisserver.models; import org.springframework.stereotype.Component; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.io.BufferedReader; -import java.io.File; import java.io.FileReader; import java.io.IOException; -import org.apache.catalina.core.ApplicationContext; -import com.ntlab.irisserver.utils.Base64Decode; -import org.apache.catalina.core.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.beans.BeansException; - import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @Component public class KeywordManager { + private static KeywordManager theInstance = null; String[] keywords = {"0"}; - /*= {"りんご", - "ごりら","らっぱ","ぱんつ","つみき",};*/ - private List FileRead(String path) { - List ss = new ArrayList(); - - try { - BufferedReader buffReader = - new BufferedReader(new FileReader(path)); - String s; - - while ((s = buffReader.readLine()) != null) { - ss.add(s); - } - buffReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - return ss; - - } - + //シングルトンパターンでインスタンス作成 public static KeywordManager getInstance() { if(theInstance == null) { theInstance = new KeywordManager(); @@ -56,6 +24,8 @@ return theInstance; } + //------------------------------------------------------------------------------ + //読み込んだファイルのキーワードをStringの配列で返す public String[] getKeywords(String path){ var response = Response.status(Response.Status.NO_CONTENT); @@ -63,11 +33,34 @@ response.status(400).entity("パスがありません"); throw new WebApplicationException(response.build()); } else if(FileRead(path).size() == 0){ - response.status(400).entity(path+"に対応したファイルがありません"); + response.status(404).entity(path+"に対応したファイルがありません"); throw new WebApplicationException(response.build()); } else { keywords = FileRead(path).toArray(new String[FileRead(path).size()]); } + return keywords; } + + //----------------------------------------------------------------------------- + //pathのファイルを読み込み + private List FileRead(String path) { + List ss = new ArrayList(); + + try { + BufferedReader buffReader = new BufferedReader(new FileReader(path)); + String s; + while ((s = buffReader.readLine()) != null) { + ss.add(s); + } + + buffReader.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return ss; + } + }