commit | author | age
|
86bbf7
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.mediaz.api; |
|
7 |
|
|
8 |
import com.sun.net.httpserver.HttpExchange; |
|
9 |
import de.uhilger.mediaz.App; |
|
10 |
import de.uhilger.mediaz.Server; |
|
11 |
import de.uhilger.mediaz.store.StorageFile; |
|
12 |
import java.io.File; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.OutputStream; |
|
15 |
import java.util.ArrayList; |
|
16 |
import java.util.logging.Logger; |
|
17 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
* @author ulrich |
|
21 |
*/ |
|
22 |
public class ListFileHandler extends FileHandler { |
|
23 |
|
|
24 |
/* Der Logger fuer diesen ListFileHandler */ |
|
25 |
private static final Logger logger = Logger.getLogger(ListFileHandler.class.getName()); |
|
26 |
|
|
27 |
public ListFileHandler(String absoluteDirectoryPathAndName) { |
|
28 |
super(absoluteDirectoryPathAndName); |
|
29 |
} |
|
30 |
|
|
31 |
@Override |
|
32 |
public void handle(HttpExchange e) throws IOException { |
|
33 |
String path = e.getRequestURI().toString(); |
|
34 |
logger.fine(path); |
|
35 |
if(path.endsWith(App.getRs(Server.RB_SLASH))) { |
|
36 |
String fName = getFileName(e); |
|
37 |
logger.fine(fName); |
|
38 |
File dir = new File(fileBase, fName); |
|
39 |
logger.fine(dir.getAbsolutePath()); |
|
40 |
File[] files = dir.listFiles(); |
|
41 |
ArrayList list = new ArrayList(); |
|
42 |
if(files != null) { |
|
43 |
for(File file : files) { |
|
44 |
StorageFile sf = new StorageFile(); |
|
45 |
sf.setName(file.getName()); |
|
46 |
if(file.isDirectory()) { |
822ddf
|
47 |
sf.setTyp(StorageFile.TYP_FOLDER); |
86bbf7
|
48 |
} else { |
822ddf
|
49 |
sf.setTyp(StorageFile.TYP_FILE); |
86bbf7
|
50 |
} |
U |
51 |
list.add(sf); |
|
52 |
} |
|
53 |
} |
|
54 |
//Gson gson = new Gson(); |
|
55 |
//String json = gson.toJson(fileNames); |
|
56 |
String json = jsonWithCustomType(list, "Medialiste"); |
|
57 |
logger.fine(json); |
|
58 |
e.sendResponseHeaders(200, json.length()); |
|
59 |
OutputStream os = e.getResponseBody(); |
|
60 |
os.write(json.getBytes()); |
|
61 |
os.close(); |
|
62 |
} else { |
|
63 |
super.handle(e); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
} |