Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, we want to apply our knowledge of inheritance, polymorphism,

ID: 3885615 • Letter: I

Question

In this assignment, we want to apply our knowledge of inheritance, polymorphism, and interfaces to build a very basic
web server. A web server is a software that receives requests from client web browsers (e.g., Firefox, Chrome). Upon
receiving a request, it responds accordingly. For instance, a client browser may request to get a page from the web
server. You are to design the basic primitives of a web server to simulate its behavior. Your design must be flexible so
that any integration of a new feature is easily incorporated.
Our web server works as follows:

1. The server has a set of resources each having a unique URI (Unique Resource Identifier). A URI can be
modeled as a string denoting the location of the resource. For example, “/employees/1”, or “/index.html”

2. The resources can be files, services, or entities. A service performs a given task such as multiplying two input
numbers. An entity represents a record of information. For example, we can have an entity for employees,
another entity for secretaries, and another for lawyers, etc. It depends on what kind of data is stored in the
server.

3. The server receives requests of different types (we use the verbs of the HTTP protocol):
• GET: to read a resource. All resources are readable, but they differ in the way they are read. For
example, an entity is read as a string representation of the entity. A service is read as a description of
the service it offers. A file is read by opening the file and responding with all its content (We assume
that all files are text (ASCII) files in this assignment).
• POST: to create a new resource, or to ask for a service. It applies for creating files and entities or
requesting a service.
• PUT: to update all the fields of an existing resource. It only applies for entities.
• PATCH: to update a special field of an existing resource. Each request must have the id of the
requested resource. This only applies for entities.
• DELETE: to delete a resource. For an entity, it will just be deleted from the entity array. For a service,
it is not exposed any more in the list of services. For a file, it is deleted from the disk.

4. The server implements a set of parsers and a set of controllers to respond to the received requests. Each
parser is specialized for a given request type. Each controller is specialized for a given request type and
potentially a given resource type.

5. The communication channel with the client is the standard I/O. You are not asked to implement any network
communications. In other words, the server receives a request as a one (or more) line(s) from the standard
input. The line starts with the request type (i.e. GET, POST, etc.) followed by the URI of the resource and then
by additional parameters (e.g. entity in comma separated values, text file content, service arguments, ...). The
server responds by printing to the standard output.

Example of server main code:

import java.util.Scanner;

public class Server {

private Scanner scan = new Scanner(System.in);

public void run() {
while (scan.hasNextLine()) {

String line = scan.nextLine();

if (line.length() == 0) {

continue; }

String[] parts = line.split(" ");

if (parts[0].equals("GET")) {

// GET Parser

// GET Controller

} else if (parts[0].equals("POST")) {

// POST Parser

// POST Controller

} else if (parts[0].equals("PUT")) {

// PUT Parser

// PUT Controller

} else if (parts[0].equals("PATCH")) {

// PATH Parser

// PATCH Controller

} else if (parts[0].equals("DELETE")) {

// DELETE Parser

// DELETE Controller

} else {
System.err.println("Method Not Supported");

}

}

}
public static void main(String[] args) {

(new Server()).run();

}

Explanation / Answer

webServ.java

/*
* 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 webserver;

/**
*
* @author Akshay Bisht
*/
import java.util.*;
import java.io.*;
import java.net.*;

public class webServ {
private static ServerSocket servSocks;

public static void main(String[] args) throws IOException {
servSocks=new ServerSocket(80);
while (true) {
try {
Socket soc=servSocks.accept();
new clientHand(soc);
}
catch (Exception e) {
System.out.println(e);
}
}
}
}

class clientHand extends Thread {
private Socket socket;
public clientHand(Socket soc) {
socket=soc;
start();
}
public void run() {
try {
BufferedReader buff=new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintStream pst=new PrintStream(new BufferedOutputStream(
socket.getOutputStream()));
String soc=buff.readLine();
System.out.println(soc);
String fName="";
StringTokenizer st=new StringTokenizer(soc);
try {
if (st.hasMoreElements() && st.nextToken().equalsIgnoreCase("GET")
&& st.hasMoreElements())
fName=st.nextToken();
else
throw new FileNotFoundException();
if (fName.endsWith("/"))
fName+="index.html";
while (fName.indexOf("/")==0)
fName=fName.substring(1);
fName=fName.replace('/', File.separator.charAt(0));
if (fName.indexOf("..")>=0 || fName.indexOf(':')>=0
|| fName.indexOf('|')>=0)
throw new FileNotFoundException();
if (new File(fName).isDirectory()) {
fName=fName.replace('\', '/');
pst.print("HTTP/1.0 301 Moved Permanently "+
"Location: /"+fName+"/ ");
pst.close();
return;
}
InputStream ist=new FileInputStream(fName);
String mmTyp="text/plain";
if (fName.endsWith(".html") || fName.endsWith(".htm"))
mmTyp="text/html";
else if (fName.endsWith(".jpg") || fName.endsWith(".jpeg"))
mmTyp="image/jpeg";
else if (fName.endsWith(".gif"))
mmTyp="image/gif";
else if (fName.endsWith(".class"))
mmTyp="application/octet-stream";
pst.print("HTTP/1.0 200 OK ob"+
"Content-type: "+mmTyp+" ob ");
byte[] b=new byte[4096];
int nob;
while ((nob=ist.read(b))>0)
pst.write(b, 0, nob);
pst.close();
}
catch (FileNotFoundException e) {
pst.println("HTTP/1.0 404 Not Found "+
"Content-type: text/html "+
"<html><head></head><body>"+fName+" not found</body></html> ");
pst.close();
}
}
catch (IOException e) {
System.out.println(e);
}
}
}

Rate an upvote....Thankyou

Hope this helps.....