Client가 웹브라우저를 통해 서버에 접속하면 Agentlet과 RequestInputStream, RequsetOutputStream이 생김.

Agentlet은 고객의 접속 주소를 parsing해 나온 문자에 따라 그에 맞는 하위 클래스(Strict, static 등)로 생성됨(Agentlet은 추상 클래스).

Client가 접속하면 RequsetOutputStream이 접속 주소를 잘라내고 그 내용을 인스턴스변수로 가지고 있음. 

생성된 Agentlet 하위 객체가 doJob을 함(추상 클래스에 header가 있음)




package http2;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Scanner;


public class Mp3Server {

private ServerSocket serverSocket;

public Mp3Server(int port) throws Exception{

this.serverSocket = new ServerSocket(port);

System.out.println("ready............");

readyAgent();

mainLoop();

}

public void readyAgent(){

//agentMap.put("/list", new ListAgentlet());

}

public void mainLoop()throws Exception{

ServerSocket server = this.serverSocket;

while(true){

Socket socket = server.accept();

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

try{

Agentlet agent = null;

RequestInputStream request = new RequestInputStream(in);

ResponseOutputStream response = new ResponseOutputStream(out,new ByteArrayOutputStream());

if(request.getExt().equals("htm") == false){

agent = new StrictAgentlet();

}

agent.doJob(request, response);

response.getBos().writeTo(response.getOut());

}catch(Exception e){

System.out.println(e.getMessage());

e.printStackTrace();

}finally{

try{ in.close(); }catch(Exception e){}

try{ out.close(); }catch(Exception e){}

try{ socket.close(); }catch(Exception e){}

}

}

//this.serverSocket.close();

}

public static void main(String[] args) throws Exception{

Mp3Server server = new Mp3Server(5555);

}

}




-------------------------------------------------------------------------------------------------



package http2;


import java.io.OutputStream;

import java.util.HashMap;

import java.util.Map;


public abstract class Agentlet {

static Map<String, String> mimeMap;

static {

mimeMap = new HashMap<String, String>();

mimeMap.put("mp3","audio/mpeg3");

mimeMap.put("jpg","image/jpeg");

}


public void makeResponseHearder(String ext, int size,  ResponseOutputStream res)throws Exception{

OutputStream out = res.getOut();

out.write(new String("HTTP/1.1 200 OK\r\n").getBytes());

out.write(new String("Cache-Control: private\r\n").getBytes()); 

    out.write(new String("Content-Length: "+size+"\r\n").getBytes()); 

    out.write(new String("Content-Type: "+ mimeMap.get(ext)+"; charset=UTF-8\r\n\r\n").getBytes());

}

public abstract void doJob(

RequestInputStream request, 

ResponseOutputStream response)throws Exception;

}




package http2;


import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;


public class StrictAgentlet extends Agentlet {

private static final String ROOT = "C:\\zzz\\mp3";

@Override

public void doJob(RequestInputStream request, ResponseOutputStream response)

throws Exception {

// TODO Auto-generated method stub

// /aaa.mp3

String targetFileName = request.getUrlPage();

String ext = request.getExt();

System.out.println("ext :" + ext);

ByteArrayOutputStream bos = response.getBos();

File targetFile = new File(ROOT+targetFileName.replaceAll("//", File.pathSeparator));

System.out.println(targetFile);

CopyUtil.copy(new FileInputStream(targetFile), bos, true, false);

makeResponseHearder(ext, bos.size(), response);

}

}



---------------------------------------------------------------------------------------------------



package http2;

import java.io.InputStream;

import java.util.Scanner;


public class RequestInputStream {

private InputStream in;

private Scanner scanner;

private String urlPattern;

private String urlPage;

private String ext;

private String query;

public RequestInputStream(InputStream in){

this.in = in;

this.scanner = new Scanner(in);

parsingRequest(scanner.nextLine());

}

private void parsingRequest(String dataStr) {

//GET / HTTP1.1

String[] arr = dataStr.split(" ");

this.urlPattern =  arr[1];

String[] outerArr = urlPattern.split("\\?");

this.urlPage = outerArr[0];

this.ext = outerArr[0].substring(outerArr[0].indexOf(".")+1);

if(outerArr.length > 1){

this.query = outerArr[1];

}

}

public InputStream getIn() {

return in;

}

public Scanner getScanner() {

return scanner;

}

public String getUrlPattern() {

return urlPattern;

}

public String getUrlPage() {

return urlPage;

}

public String getExt() {

return ext;

}

public String getQuery() {

return query;

}

}




package http2;

import java.io.ByteArrayOutputStream;

import java.io.OutputStream;


public class ResponseOutputStream {

private OutputStream out;

private ByteArrayOutputStream bos;

public ResponseOutputStream(OutputStream out, ByteArrayOutputStream bos) {

super();

this.out = out;

this.bos = bos;

}

public OutputStream getOut() {

return out;

}

public ByteArrayOutputStream getBos() {

return bos;

}

}



Posted by 타다키치

댓글을 달아 주세요