Web Science/Part1: Foundations of the web/Hypertext Transfer Protocol/Content negotiation

Content negotiation

Learning goals

no learning goals defined
You can define learning goals here.
In general you can use the edit button in the upper right corner of a section to edit its content.

Video

Script

Mainly the following function was added to SimpleWebServer.java. There are some slighte adoptions to the Request class but this is mainly code completion displayed inside the video.

	private static String fetchAsciiFile(String path, String accept) {
		try {
			StringBuilder body = new StringBuilder(100000);
			BufferedReader br = new BufferedReader(new FileReader(new File(System.getProperty("user.dir") + "/webdirectory" + path)));
			String tmp = null; 
			
			if (accept != null && accept.equals("text/xml")){
				body.append("<?xml version=\"1.0\" encoding=\"ASCII\" standalone=\"yes\"?>\n<playlist>\n");
			}
			
			while ((tmp = br.readLine())!= null){
				if (accept == null)
					body.append(tmp + "\n");
				else if (accept.equals("text/plain")){
					body.append(tmp + "\n");
				} else if (accept.equals("text/xml")){
					String []values = tmp.split(" - ");
					body.append("<track id=\""+values[1]+"\">\n\t<time>"+values[0]+"</time>\n\t<artist>"+values[2]+"</artist>\n\t<title>"+values[3]+"</title>\n</track>\n");
				}
			}
			if (accept != null && accept.equals("text/xml")){
				body.append("</playlist>\n");
			}

			br.close();
			return body.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

Quiz

1 Which of the following HTTP requests should be used to request an XML file from a server

GET /file HTTP/1.0\r\nAccept: text/xml\r\n\r\n
GET /file?type=xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\nAccept: text/xml\r\n\r\n

2 Which of the following HTTP requests would technically work (if implemented) to request an XML file from a server

GET /file HTTP/1.0\r\nAccept: text/xml\r\n\r\n
GET /file?type=xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\n\r\n
GET /file.xml HTTP/1.0\r\nAccept: text/xml\r\n\r\n

3 What is the main aim of content negotiation?

human to machine communication over http
human to human communication over http
machine to machine communication over http
All of the above


Discussion