/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
package de.uhilger.mediaz.api;
|
|
import com.sun.net.httpserver.HttpExchange;
|
import de.uhilger.mediaz.App;
|
import de.uhilger.mediaz.Server;
|
import de.uhilger.mediaz.store.StorageFile;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.ArrayList;
|
import java.util.logging.Logger;
|
|
/**
|
*
|
* @author ulrich
|
*/
|
public class ListFileHandler extends FileHandler {
|
|
/* Der Logger fuer diesen ListFileHandler */
|
private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName());
|
|
public ListFileHandler(String absoluteDirectoryPathAndName) {
|
super(absoluteDirectoryPathAndName);
|
}
|
|
@Override
|
public void handle(HttpExchange e) throws IOException {
|
String path = e.getRequestURI().toString();
|
logger.fine(path);
|
if(path.endsWith(App.getRs(Server.RB_SLASH))) {
|
String fName = getFileName(e);
|
logger.fine(fName);
|
File dir = new File(fileBase, fName);
|
logger.fine(dir.getAbsolutePath());
|
File[] files = dir.listFiles();
|
ArrayList list = new ArrayList();
|
if(files != null) {
|
for(File file : files) {
|
StorageFile sf = new StorageFile();
|
sf.setName(file.getName());
|
if(file.isDirectory()) {
|
sf.setTyp(StorageFile.TYP_FOLDER);
|
} else {
|
sf.setTyp(StorageFile.TYP_FILE);
|
}
|
list.add(sf);
|
}
|
}
|
//Gson gson = new Gson();
|
//String json = gson.toJson(fileNames);
|
String json = jsonWithCustomType(list, "Medialiste");
|
logger.fine(json);
|
e.sendResponseHeaders(200, json.length());
|
OutputStream os = e.getResponseBody();
|
os.write(json.getBytes());
|
os.close();
|
} else {
|
super.handle(e);
|
}
|
}
|
|
|
}
|