public OSGiCommand getCmd() {
if (path.equals("/bundle") ||
path.equals("/bundles") ||
path.equals("/bundle/json") ||
path.equals("/list.html"))
return new OSGiListCommand(context, new PrintWriter(out));
else {
String prefix = null;
String commandToValidate = null;
try {
prefix = path.split("\\/")[1].toLowerCase();
commandToValidate = path.split("\\/")[2].toUpperCase();
//System.out.println("prefix: " + prefix + "\ncommand: " + commandToValidate + "\nID: " + getBundleID());
} catch (ArrayIndexOutOfBoundsException aioob) {
}
if (prefix.equals("bundle")) {
if (commandToValidate.equals("START")) {
OSGiStartCommand startCommand = new OSGiStartCommand(context, new PrintWriter(out));
startCommand.setBundleID(getBundleID());
return startCommand;
}
if (commandToValidate.equals("STOP")) {
OSGiStopCommand stopCommand = new OSGiStopCommand(context, new PrintWriter(out));
stopCommand.setBundleID(getBundleID());
return stopCommand;
}
if (commandToValidate.equals("UNINSTALL")) {
OSGiUninstallCommand uninstallCommand = new OSGiUninstallCommand(context, new PrintWriter(out));
uninstallCommand.setBundleID(getBundleID());
return uninstallCommand;
}
if (commandToValidate.equals("INSTALL")) {
OSGiInstallCommand installCommand = new OSGiInstallCommand(context, new PrintWriter(out));
installCommand.setUrl(installCommand.decodeURL(path.split("\\/")[3]));
return installCommand;
}
if (commandToValidate.equals("DETAILS")) {
OSGiDetailsCommand detailsCommand = new OSGiDetailsCommand(context, new PrintWriter(out));
detailsCommand.setBundleID(getBundleID());
return detailsCommand;
}
if (commandToValidate.equals("UPDATE")) {
OSGiUpdateCommand updateCommand = new OSGiUpdateCommand(context, new PrintWriter(out));
updateCommand.setBundleID(getBundleID());
String url64;
try {
url64 = path.split("\\/")[4];
String url = updateCommand.decodeURL(url64);
updateCommand.setUrl(url);
} catch (Exception e) {
e.printStackTrace();
}
return updateCommand;
}
if (commandToValidate.equals("STATE")) {
OSGiStateCommand stateCommand = new OSGiStateCommand(context, new PrintWriter(out));
stateCommand.setBundleID(getBundleID());
return stateCommand;
}
}
else
return null;
}
return null;
}
/** Starts an installed Bundle
* @param toBeStarted Bundle to start
* @return 0 if OK , 1 on error
*/
public int startBundle(Bundle toBeStarted) {
if(toBeStarted != null && toBeStarted.getState() != Bundle.UNINSTALLED) {
try {
toBeStarted.start();
} catch (BundleException e) {
e.printStackTrace();
}
}
else {
return 1;
}
return 0;
}
public class Service extends Thread {
public Service(File rootDir, int port) {
try {
_rootDir = rootDir.getCanonicalFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
_serverSocket = new ServerSocket(port);
System.out.println("WEBUI started on port:" + port);
} catch (BindException be) {
try {
System.out.println("Wait...");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
start();
}
public void run() {
Socket socket=null;
while (_running) {
try {
socket = _serverSocket.accept();
RequestThread requestThread = new RequestThread(socket, _rootDir, context);
requestThread.start();
} catch (IOException e) {
run();
} catch (NullPointerException npe) {
try {
System.out.println("Wait...");
Thread.sleep(5000);
run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private File _rootDir;
private ServerSocket _serverSocket;
public boolean _running = true;
}
public class RequestThread extends Thread {
private BufferedOutputStream out;
private File _rootDir;
private Socket _socket;
public RequestThread(Socket socket, File rootDir) {
_socket = socket;
_rootDir = rootDir;
}
public void run() {
InputStream reader = null;
try {
_socket.setSoTimeout(30000);
BufferedReader in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
out = new BufferedOutputStream(_socket.getOutputStream());
String request = in.readLine();
if (request == null || !request.startsWith("GET ") || !(request.endsWith(" HTTP/1.0") || request.endsWith("HTTP/1.1"))) {
// Invalid request type (not "GET")
sendError(out, 500, "Invalid Method.");
return;
}
String path = request.substring(4, request.length() - 9);
File file = new File(_rootDir, URLDecoder.decode(path, "UTF-8")).getCanonicalFile();
if(isLegalFile(file, path)) {
serveBigPage(reader, file);
}
out.flush();
out.close();
}
catch (IOException e) {
if (reader != null) {
try {
reader.close();
}
catch (Exception anye) {
// Do nothing.
}
}
}
}
private void serveBigPage(InputStream reader, File file) throws IOException {
reader = new BufferedInputStream(new FileInputStream(file));
String contentType = (String) Daemon.MIME_TYPES.get(HTTPd.getExtension(file));
if (contentType == null) {
contentType = "application/octet-stream";
}
sendHeader(out, 200, contentType, file.length(), file.lastModified());
byte[] buffer = new byte[128];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
reader.close();
}
}