Skip to content

Instantly share code, notes, and snippets.

@uraimo
Created May 26, 2022 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uraimo/36874b464e176907dddf238505c0b2f1 to your computer and use it in GitHub Desktop.
Save uraimo/36874b464e176907dddf238505c0b2f1 to your computer and use it in GitHub Desktop.
Interview question[entry-level, java]: Review the following samples and spot issues (5+ each)

Sample 1

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;
   }

Sample 2

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;

}

Sample 3

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();
   }   
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment