use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.
the class QuestionJobResourceReferenceHandler method handle.
@Override
public void handle(ParentResourceReference reference) throws ResourceReferenceHandlerException {
List<String> jobId = reference.getPathSegments();
Job job = this.executor.getJob(jobId);
if (job == null) {
throw new ResourceReferenceHandlerException("Cannot find any running job with id " + jobId);
}
Object question = job.getStatus().getQuestion();
if (question == null) {
throw new ResourceReferenceHandlerException("The job with id " + jobId + " does not have any question");
}
Request request = this.container.getRequest();
String prefix = "question/";
String contentType = "text/html; charset=utf-8";
// POST request means answer
if (request instanceof ServletRequest) {
HttpServletRequest httpRequest = ((ServletRequest) request).getHttpServletRequest();
if (httpRequest.getMethod().equals("POST")) {
String token = httpRequest.getParameter("form_token");
// TODO: should probably move this check in some filter triggered by an annotation
if (this.csrf.isTokenValid(token)) {
answer(httpRequest, job, jobId, question);
prefix = "answer/";
contentType = "application/json";
} else {
// TODO: Throw some exception
}
}
}
String jobType = job.getType();
// Provide informations about the job to the template
this.scriptContextManager.getCurrentScriptContext().setAttribute("job", job, ScriptContext.ENGINE_SCOPE);
String[] templates = getTemplates(question.getClass(), jobType, prefix);
if (!tryTemplates(contentType, templates)) {
throw new ResourceReferenceHandlerException("Cannot find any template for the job with id" + jobId + " (" + Arrays.toString(templates) + ")");
}
}
use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.
the class ContextAndActionURLNormalizer method getActionServletMapping.
/**
* Get the path prefix used for the Struts Action Servlet, either a prefix similar to the one used in the current
* request if it also passes through the Action servlet, or using the default path configured for it.
*
* @return a path used for triggering the Struts Action Servlet (may be the empty string)
*/
protected String getActionServletMapping() {
String result = this.defaultServletMapping;
if (this.container.getRequest() instanceof ServletRequest) {
HttpServletRequest hsRequest = ((ServletRequest) this.container.getRequest()).getHttpServletRequest();
result = StringUtils.strip(hsRequest.getServletPath(), IGNORED_MAPPING_CHARACTERS);
if (!this.validServletMappings.contains(result)) {
// The current request doesn't pass through the Action servlet, don't reuse the path prefix
result = this.defaultServletMapping;
}
}
return result;
}
use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.
the class DefaultCSRFTokenTest method configure.
@Before
public void configure() throws Exception {
// set up mocked dependencies
// document access bridge
final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
final CopyStringMatcher returnValue = new CopyStringMatcher(resubmitUrl + "?", "");
getMockery().checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNonNull(DocumentReference.class)), with("view"), with(returnValue), with(aNull(String.class)));
will(returnValue);
allowing(mockDocumentAccessBridge).getDocumentURL(with(aNull(DocumentReference.class)), with("view"), with(aNull(String.class)), with(aNull(String.class)));
will(returnValue(mockDocumentUrl));
allowing(mockDocumentAccessBridge).getCurrentDocumentReference();
will(returnValue(null));
}
});
// configuration
final CSRFTokenConfiguration mockConfiguration = getComponentManager().getInstance(CSRFTokenConfiguration.class);
getMockery().checking(new Expectations() {
{
allowing(mockConfiguration).isEnabled();
will(returnValue(true));
}
});
// request
final HttpSession mockSession = getMockery().mock(HttpSession.class);
final HttpServletRequest httpRequest = getMockery().mock(HttpServletRequest.class);
final ServletRequest servletRequest = new ServletRequest(httpRequest);
getMockery().checking(new Expectations() {
{
allowing(httpRequest).getRequestURL();
will(returnValue(new StringBuffer(mockDocumentUrl)));
allowing(httpRequest).getRequestURI();
will(returnValue(mockDocumentUrl));
allowing(httpRequest).getParameterMap();
will(returnValue(new HashMap<String, String[]>()));
allowing(httpRequest).getSession();
will(returnValue(mockSession));
}
});
// session
getMockery().checking(new Expectations() {
{
allowing(mockSession).getAttribute(with(any(String.class)));
will(returnValue(new HashMap<String, Object>()));
}
});
// container
final Container mockContainer = getComponentManager().getInstance(Container.class);
getMockery().checking(new Expectations() {
{
allowing(mockContainer).getRequest();
will(returnValue(servletRequest));
}
});
// logging
getMockery().checking(new Expectations() {
{
// Ignore all calls to debug()
ignoring(any(Logger.class)).method("debug");
}
});
this.csrf = getComponentManager().getInstance(CSRFToken.class);
}
use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.
the class DefaultXWikiContextInitializer method initialize.
@Override
public XWikiContext initialize(ExecutionContext econtext) throws XWikiException {
Request request = this.container.getRequest();
XWikiContext xcontext;
if (!(request instanceof ServletRequest)) {
if (this.fallbackOnStub) {
xcontext = this.contextProvider.createStubContext();
} else {
throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Unsupported request type [" + request.getClass() + "]");
}
} else {
try {
HttpServletRequest httpServletRequest = ((ServletRequest) request).getHttpServletRequest();
HttpServletResponse httpServletReponse = ((ServletResponse) this.container.getResponse()).getHttpServletResponse();
xcontext = initializeXWikiContext(httpServletRequest, httpServletReponse);
// Put the XWikiContext in the ExecutionContext in case the authenticator needs it
if (econtext != null) {
xcontext.declareInExecutionContext(econtext);
}
if (this.authenticate) {
authenticate(xcontext);
}
} catch (XWikiException e) {
if (this.fallbackOnStub) {
xcontext = this.contextProvider.createStubContext();
} else {
throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Failed to initialize XWikiContext", e);
}
}
}
// Put the XWikiContext in the ExecutionContext
if (econtext != null) {
xcontext.declareInExecutionContext(econtext);
}
return xcontext;
}
use of org.xwiki.container.servlet.ServletRequest in project xwiki-platform by xwiki.
the class XWikiServerXwikiDocumentInitializer method updateDocument.
@Override
public boolean updateDocument(XWikiDocument document) {
boolean needsUpdate = super.updateDocument(document);
// Add a descriptor if none already exist
if (document.getXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS) == null) {
XWikiContext xcontext = this.contextProvider.get();
try {
BaseObject xobject = document.newXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS, xcontext);
xobject.setLargeStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION, "Main wiki");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE, "Main.WebHome");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_LANGUAGE, "en");
xobject.setIntValue(XWikiServerClassDocumentInitializer.FIELD_SECURE, 0);
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_STATE, "active");
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_VISIBILITY, "public");
xobject.setLargeStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER, XWikiRightService.SUPERADMIN_USER_FULLNAME);
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, "Home");
Request request = this.container.getRequest();
if (request instanceof ServletRequest) {
ServletRequest servletRequest = (ServletRequest) request;
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER, servletRequest.getHttpServletRequest().getServerName());
} else {
xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER, "localhost");
}
needsUpdate = true;
} catch (XWikiException e) {
this.logger.error("Faied to initialize main wiki descriptor", e);
}
}
return needsUpdate;
}
Aggregations