Merge pull request #66 from nitta-lab-2022/KeywordManager
KeywordManagerでとりあえずプロジェクト内のファイルを読み取る機能を追加しました
commit 785aa43d6e6c517353708ce539c1d2e8bc75e93a
2 parents db476bf + e9ccd80
Kengo Iwamoto authored on 7 Jun 2022
Showing 3 changed files
View
69
src/main/java/com/ntlab/irisserver/models/KeywordManager.java
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 = {"りんご",
"ごりら","らっぱ","ぱんつ","つみき"};
String[] keywords = {"0"};
/*= {"りんご",
"ごりら","らっぱ","ぱんつ","つみき",};*/
 
private List<String> FileRead(String path) {
List<String> ss = new ArrayList<String>();
 
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();
}
return theInstance;
}
 
public String[] getKeywords(){
public String[] getKeywords(String path){
var response = Response.status(Response.Status.NO_CONTENT);
 
if(path == null){
response.status(400).entity("パスがありません");
throw new WebApplicationException(response.build());
} else if(FileRead(path).size() == 0){
response.status(400).entity(path+"に対応したファイルがありません");
throw new WebApplicationException(response.build());
} else {
keywords = FileRead(path).toArray(new String[FileRead(path).size()]);
}
return keywords;
}
}
View
8
src/main/java/com/ntlab/irisserver/models/keywords.txt 0 → 100644
りんご
ごりら
らっぱ
パイナップル
ルビー
ビール
ルアー
View
35
src/main/java/com/ntlab/irisserver/resources/KeywordsRest.java
package com.ntlab.irisserver.resources;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.ntlab.irisserver.models.KeywordManager;
 
 
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
 
 
@Component
@Path("/keywords")
 
public class KeywordsRest {
public class KeywordsRest implements ApplicationContextAware {
 
private ApplicationContext applicationContext;
 
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
 
@GET
@Produces(MediaType.APPLICATION_JSON)
public String[] getKeywords(){
public String[] getKeywords() throws IOException {
 
String path;
path = applicationContext.getResource("file:").getFile().getAbsolutePath()
+"/apache-tomcat-9.0.10/webapps/iris/WEB-INF/classes/com/ntlab/irisserver/models/keywords.txt";
 
//path = "C:\\Users\\student\\Desktop\\IrisServer\\src\\main\\java\\com\\ntlab\\irisserver\\models\\keywords.txt";
KeywordManager km = KeywordManager.getInstance();
String[] keywords = km.getKeywords();
String[] keywords = km.getKeywords(path);
 
return keywords;
}
 
 
}