📄 httpstatus.java
字号:
/**
*
*/
package edu.sysu.http.impl;
import java.util.HashMap;
import edu.sysu.http.util.HttpGrammarException;
import edu.sysu.http.util.HttpRegex;
/**
* @author Administrator
*
* Status = status-code SP reason-phrase
*/
public class HttpStatus {
public static HttpStatus.Status SC_OK;
public static HttpStatus.Status SC_ACCEPTED;
public static HttpStatus.Status SC_BAD_REQUEST;
public static HttpStatus.Status SC_FORBIDDEN;
public static HttpStatus.Status SC_NOT_FOUND;
public static HashMap<String, String> MapStatus = new HashMap<String, String>();
static {
try {
HttpStatus.SC_OK = new HttpStatus.Status("200", "OK");
MapStatus.put(HttpStatus.SC_OK.getStatusCode(), HttpStatus.SC_OK
.getReasonPhrase());
HttpStatus.SC_ACCEPTED = new HttpStatus.Status("202", "Accepted");
MapStatus.put(HttpStatus.SC_ACCEPTED.getStatusCode(),
HttpStatus.SC_ACCEPTED.getReasonPhrase());
HttpStatus.SC_BAD_REQUEST = new HttpStatus.Status("400",
"Bad Request");
MapStatus.put(HttpStatus.SC_BAD_REQUEST.getStatusCode(),
HttpStatus.SC_BAD_REQUEST.getReasonPhrase());
HttpStatus.SC_FORBIDDEN = new HttpStatus.Status("403", "Forbidden");
MapStatus.put(HttpStatus.SC_FORBIDDEN.getStatusCode(),
HttpStatus.SC_FORBIDDEN.getReasonPhrase());
HttpStatus.SC_NOT_FOUND = new HttpStatus.Status("404", "Not Found");
MapStatus.put(HttpStatus.SC_NOT_FOUND.getStatusCode(),
HttpStatus.SC_NOT_FOUND.getReasonPhrase());
} catch (Exception e) {
}
}
/**
*
*/
public HttpStatus() {
// TODO Auto-generated constructor stub
}
/**
* static class for Singleton design pattern, instance of HttpStatus will be
* created when the first time you make a call to HttpStatus.getInstance
*
* @author Cyberpet
* @see HttpStatus#getInstance()
*/
// static class HttpStatusSingletonHolder {
// static HttpStatus instance = new HttpStatus();
// }
/**
* get the only instance of HttpStatus
*
* @return HttpStatus
* @see HttpStatus
*/
// public static HttpStatus getInstance() {
// return HttpStatusSingletonHolder.instance;
// }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(HttpStatus.SC_NOT_FOUND.getStatusCode() + " "
+ HttpStatus.SC_NOT_FOUND.getReasonPhrase());
System.out.println(HttpStatus.SC_OK.getStatusCode() + " "
+ HttpStatus.SC_OK.getReasonPhrase());
System.out.println(HttpStatus.SC_NOT_FOUND.toString());
}
public static class Status {
private String statusCode;
private String reasonPhrase;
private final HttpRegex regexCode = new HttpRegex(HttpRules.DIGIT
+ "{3}");
public final HttpRegex getRegexCode() {
return regexCode;
}
private HttpRegex regexPhrase = new HttpRegex("[" + HttpRules.TEXT
+ "\\" + HttpRules.SP + "&&[^\\" + HttpRules.CR + "\\"
+ HttpRules.LF + "]]+");
public HttpRegex getRegexPhrase() {
return regexPhrase;
}
public Status() {
}
public Status(String code, String phrase) throws HttpGrammarException {
this.setStatusCode(code);
this.setReasonPhrase(phrase);
}
/**
* @param statusCode
* the statusCode to set
*/
public void setStatusCode(String statusCode)
throws HttpGrammarException {
if (regexCode.Match(statusCode))
this.statusCode = statusCode;
else
throw new HttpGrammarException("Status-Code invalid.");
}
/**
* @return the statusCode
*/
public String getStatusCode() {
return statusCode;
}
/**
* @param reasonPhrase
* the reasonPhrase to set
*/
public void setReasonPhrase(String reasonPhrase)
throws HttpGrammarException {
if (regexPhrase.Match(reasonPhrase))
this.reasonPhrase = reasonPhrase;
else
throw new HttpGrammarException("Reason-Phrase invalid.");
}
/**
* @return the reasonPhrase
*/
public String getReasonPhrase() {
return reasonPhrase;
}
public String toString() {
return this.getStatusCode() + HttpRules.SP + this.getReasonPhrase();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -