我有一个旨在发挥搜索引擎作用的Web应用程序。它的后端是用Java开发的,它的前端是angularJS。 我在以下链接中找到了名为PrettyFacesWrappedRequest的以下类,其中包含一些解释:link here
public class PrettyFacesWrappedRequest extends HttpServletRequestWrapper{
private final Map<String, String[]> modifiableParameters;
private Map<String, String[]> allParameters = null;
/**
* Create a new request wrapper that will merge additional parameters into
* the request object without prematurely reading parameters from the
* original request.
*
* @param request
* @param additionalParams
*/
public PrettyFacesWrappedRequest(final HttpServletRequest request,
final Map<String, String[]> additionalParams)
{
super(request);
modifiableParameters = new TreeMap<String, String[]>();
modifiableParameters.putAll(additionalParams);
}
@Override
public String getParameter(final String name)
{
String[] strings = getParameterMap().get(name);
if (strings != null)
{
return strings[0];
}
return super.getParameter(name);
}
@Override
public Map<String, String[]> getParameterMap()
{
if (allParameters == null)
{
allParameters = new TreeMap<String, String[]>();
allParameters.putAll(super.getParameterMap());
allParameters.putAll(modifiableParameters);
}
//Return an unmodifiable collection because we need to uphold the interface contract.
return Collections.unmodifiableMap(allParameters);
}
@Override
public Enumeration<String> getParameterNames()
{
return Collections.enumeration(getParameterMap().keySet());
}
@Override
public String[] getParameterValues(final String name)
{
return getParameterMap().get(name);
}
}
我的目标是注入以下代码,以便管理来自任何其他应用程序的任何http请求。例如,我有以下html表单,我想搜索一个单词:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="http://localhost:8050/Babel/#/search" method="GET" enctype="application/json">
<input type="text" name="word" id="word">
<input type="submit" value="Search" >
</form>
</body>
</html>
我实现了一个类,我通过请求头的引用(request.getHeader(“referer”))检索word参数,并通过将字符串拆分为子字符串除以“=”字符来调低字符串。
以下是代码:
public class ControllerTemplate<T extends SearchResponse> {
private static final Logger LOGGER = LoggerFactory.getLogger(ControllerTemplate.class);
private static final String X_REQUESTED_WITH = "X-Requested-With";
private static final String XML_HTTP_REQUEST = "XMLHttpRequest";
protected Identification identification;
protected SearchApiUseCase<T> useCase;
protected ObjectMapper mapper;
protected ControllerTemplate(SearchApiUseCase<T> useCase, Identification identification) {// if(useCase == null || identification == null){
if(useCase == null){
throw new IllegalArgumentException("ControllerTemplate construct error");
}
this.useCase = useCase;// this.identification = identification;
this.identification = null;
this.mapper = new ObjectMapper();
}
private final ResponseEntity<?> constructResponseEntity(int httpStatusCode, String msg){
ObjectNode object = mapper.createObjectNode();
object.put("message",msg);
object.put("timestamp", Calendar.getInstance().getTimeInMillis());
return ResponseEntity.status(httpStatusCode).body(object.toString());
}
private final void isAjaxHttpRequest(HttpServletRequest httpRequest) throws HttpSearchErrorException{
if(httpRequest.getHeader(X_REQUESTED_WITH) == null || !XML_HTTP_REQUEST.equalsIgnoreCase(httpRequest.getHeader(X_REQUESTED_WITH))){
throw new HttpBadRequestSearchErrorException("X-Requested-With is missing");
}
}
private final void isUserAuthenticated() throws SearchErrorException{
identification.checkIdentity();
}
@RequestMapping(produces=MediaType.APPLICATION_JSON_UTF8_VALUE, method = {RequestMethod.GET})
@ResponseBody
public final ResponseEntity<?> research(@RequestParam(required = true, name ="word") String word,
@RequestParam(required = false, defaultValue = "1", name = "page") int page,
@RequestParam(required = false, defaultValue = "5", name = "maxrow") int maxRow,
@RequestParam(required = false, defaultValue = "", name = "filter") String filter, HttpServletRequest request) throws ServletException, IOException{
ResponseEntity<?> response = null;
System.out.println("word : " + word);
try{
isAjaxHttpRequest(request);// isUserAuthenticated();
System.out.println("the request is " + request.getQueryString());
System.out.println("Request headers : " + request.getHeader("referer"));
String referer = request.getHeader("referer");
String externalWord = referer.split("=")[1];
if(!(externalWord.equals("")) && (word.equals(""))){
word = externalWord;
page = 1;
maxRow = 5;
filter = "";
Map<String, String[]> extraParams = new TreeMap<String, String[]>();
String[] param1 = {externalWord};
String[] param2 = {"1"};
String[] param3 = {"5"};
extraParams.put("word", param1);
extraParams.put("page", param2);
extraParams.put("maxrow", param3);
HttpServletRequest wrappedRequest = new PrettyFacesWrappedRequest(request, extraParams);
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletResponse responseServ = ((ServletRequestAttributes)requestAttributes).getResponse(); request.getRequestDispatcher("http://localhost:8050/Babel/#/search").forward(wrappedRequest, responseServ);
}
SearchResponse resultat = useCase.resultUseCase(word, page, maxRow, filter, null);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST");
headers.add("Content-Type", "application/json");
headers.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
response = ResponseEntity.status(HttpStatus.OK).headers(headers).body(resultat.getSearchSources());
}catch(HttpSearchErrorException e){
LOGGER.error("{HttpSearchErrorException}",e);
response = constructResponseEntity(e.getHttpStatus(), e.getMessage());
}catch(SearchErrorException e){
LOGGER.error("{SearchErrorException}",e);
response = constructResponseEntity(HttpStatus.SERVICE_UNAVAILABLE.value(), e.getMessage());
}
System.out.println("Response : " + response.toString());
return response;
}
}
当单词作为参数提供时,研究函数通常会返回搜索结果列表。由于Web应用程序无法修改请求parameterMap,因此该函数不会返回任何结果。
很久以前,我一直试图为这个问题找到解决方案或解决方法。任何帮助将非常感激。如果需要,我甚至会提供额外的代码段。有人可以帮我吗?