Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make request data available to responders #1174

Merged
merged 2 commits into from Jan 2, 2019

Conversation

@tcnh
Copy link
Contributor

@tcnh tcnh commented Dec 27, 2018

This PR makes request data available to velocity in all available responders, so that we have access to the request data from our velocity templates. This enables the use of cookies in templates to store user preferences/settings.

To make the latter easier, a getCookie method is added to the request class, allowing to get cookie data bty its key.

src/fitnesse/http/Request.java Outdated Show resolved Hide resolved
String cookies = headers.get("cookie");
if(null != cookies) {
String[] rawCookies = cookies.split(";");
for(String cookie : rawCookies) {
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for(String cookie : rawCookies) {
for (String cookie : rawCookies) {

String[] rawCookies = cookies.split(";");
for(String cookie : rawCookies) {
String[] pair = cookie.split("=");
if(pair[0].trim().equalsIgnoreCase(name) && pair.length == 2) {
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(pair[0].trim().equalsIgnoreCase(name) && pair.length == 2) {
if (pair.length == 2 && pair[0].trim().equalsIgnoreCase(name)) {

return responseWith(contentFrom(context, request, null));
}

@Override
protected String contentFrom(FitNesseContext context, Request request, WikiPage requestedPage) {
requestData= request;
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
requestData= request;
requestData = request;

@@ -67,6 +67,20 @@ public void parse() throws HttpException {
hasBeenParsed = true;
}

public String getCookie(String name) {
String cookies = headers.get("cookie");
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getHeader()

@@ -25,9 +25,11 @@
private String qualifiedPageName;
private WikiPagePath path;
private FitNesseContext context;
private Request requestData;
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why requestData?

@@ -14,11 +14,12 @@
public class StopTestResponder implements SecureResponder {

String testId = null;
private Request requestData;
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why requestData?

@@ -30,11 +30,13 @@
public class VersionResponder implements SecureResponder {
private String version;
private String resource;
private Request requestData;
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why requestData?

@@ -16,11 +16,12 @@
import fitnesse.wiki.WikiPagePath;

public abstract class BasicResponder implements SecureResponder {
protected Request requestData;
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you call this requestData (and not just request)?

Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to put this in a field? I think it would be better if subclasses got the request as method parameter only.

velocityContext.put("theme", theme);
velocityContext.put("contextRoot", contextRoot);
velocityContext.put("request", request);
Copy link
Collaborator

@fhoeben fhoeben Dec 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider only adding the request to velocityContext on 'render()', and adding the request as parameter to html() and render()?
I think that would be nicer design-wise, as a single page could be rendered multiple times (for different requests). I think this might also make the impact on the Renders smaller, as only the methods that call html() would need the request and they seem to have this request as a parameter already.

…tra field on responders

Use existing methods to determine if a cookie header exists, eliminating the extra nullcheck
fhoeben
fhoeben approved these changes Jan 2, 2019
@fhoeben fhoeben merged commit b20aaaf into unclebob:master Jan 2, 2019
fhoeben added a commit that referenced this issue Jan 2, 2019
@tcnh tcnh deleted the requestDataInResponders branch Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants