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

added examples #374

Closed
wants to merge 10 commits into from
Closed

Conversation

VivianLopes
Copy link
Contributor

No description provided.

@VivianLopes VivianLopes requested review from kvosper and dvlato January 29, 2019 09:29
@@ -0,0 +1,78 @@
package com.hotels.styx;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a valid Java class. I will explain why further down. To avoid submitting invalid classes in pull requests, please attempt to run the associated unit test(s) first.

* This shows an example of an inteceptor that responds to a received request
*/

public class Example implements Plugin {
Copy link
Contributor

Choose a reason for hiding this comment

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

Java class names must match the file name. I would not choose either of Example (too vague) or ExampleMessageIntercept (unclear, not ending with a noun).

Perhaps EarlyReturnExamplePlugin would be a good name.

Another thing to note is that there is already a class called ExamplePlugin. That was fine when there was only one example plugin, but now that you have added another, it has become too vague. You should come up with a new name and rename it.

@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
if (request.header("X-Respond").isPresent()) {
// request.consume();
Copy link
Contributor

Choose a reason for hiding this comment

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

There should not be any commented code here.

Also please add a comment or two explaining what this code is doing - e.g. explain the branching condition and what happens in each branch.


//Example:

chain.proceed(request)
Copy link
Contributor

Choose a reason for hiding this comment

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

We have dangling Java code here that is outside of any valid part of the file. This will prevent it from compiling. Presumably it is meant to be commented too.

However, it doesn't really make sense to have all these comments/code examples underneath the class file. If we have one example as a proper class with accompanying test, shouldn't the rest of the examples be done in the same way?

* This tests the behaviours added in the example plugin.
*/

public class PluginExampleTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Java class names must match the file name. In the case of tests, we take the name of the class under test and add Test to the end.

public class PluginExampleTest {
@Test
public void returnsEarly() {
PluginExample plugin = new PluginExample();
Copy link
Contributor

Choose a reason for hiding this comment

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

The class PluginExample does not exist. Please ensure that code compiles and tests pass before creating a PR. In the case of these examples, it is important to provide code that we have confirmed functional, so as to avoid confusing anyone who tries to learn from it.

@@ -0,0 +1,50 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

The file name should end in .java

Copy link
Contributor

Choose a reason for hiding this comment

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

The filename is still incorrect

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* This shows an example of an interceptor that responds to a received request
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should contrast between the behaviour here and the alternative.

Suggested change
* This shows an example of an interceptor that responds to a received request
* This shows an example of an plugin that responds immediately to a received request, instead of proxying it downstream.


/**
* This shows an example of an interceptor that responds to a received request
* It takes a request and if the HTTP request contains an x-respond in the header then it returns an eventual made of
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* It takes a request and if the HTTP request contains an x-respond in the header then it returns an eventual made of
* It takes a request and if the HTTP request contains a header named `X-Respond`, then it returns an eventual wrapping

/**
* This shows an example of an interceptor that responds to a received request
* It takes a request and if the HTTP request contains an x-respond in the header then it returns an eventual made of
* the response code, body etc.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* the response code, body etc.
* the HTTP response.

* This shows an example of an interceptor that responds to a received request
* It takes a request and if the HTTP request contains an x-respond in the header then it returns an eventual made of
* the response code, body etc.
* The eventual object is returned but the values only come into existance whe the response return is received.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* The eventual object is returned but the values only come into existance whe the response return is received.
* The eventual object is returned immediately, even if the response has not yet arrived.

/**
* You can replace live content
* If you need to replace a message body based on information in the message headers and don't care what is in
* the message body. (E.g you need to add a HTTP message body based on a HTTP error code)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* the message body. (E.g you need to add a HTTP message body based on a HTTP error code)
* the original message body. (E.g you need to add a HTTP message body based on a HTTP error code)

* If you need to replace a message body based on information in the message headers and don't care what is in
* the message body. (E.g you need to add a HTTP message body based on a HTTP error code)
* <p>
* You can transform a live HTTP message body using a replaceWith Bytestream operator such as in the example.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* You can transform a live HTTP message body using a replaceWith Bytestream operator such as in the example.
* You can transform a live HTTP message body using the `replaceWith` Bytestream operator as shown in the example below.

}
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should be at the top of the file, combined with the other comment.

}

/**
This can be used to replace a message body without having to look into it, this will also save heap as the
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
This can be used to replace a message body without having to look into it, this will also save heap as the
* This can be used to replace a message body without having to look into it, which will also save heap space as the


/**
This can be used to replace a message body without having to look into it, this will also save heap as the
live upstream response body is never stored in heap in full
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
live upstream response body is never stored in heap in full
* live upstream response body is never stored in the heap in full.

@@ -0,0 +1,50 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

The filename is still incorrect

*Please note that this uses more heap space as the full response is transiently stored in the heap.
*/

public class ReplaceContentByAggregationExample implements Plugin {
Copy link
Contributor

Choose a reason for hiding this comment

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

I cannot find any 'modifier' implementation so I don't think this is 'usable' code. Also the ModifyHeadersExamplePluginConfig field/attribute is not used.

import static java.util.Objects.requireNonNull;

/**
* You can replace without aggregating it first, if the replacement does not depend on the original contents.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can replace... the body content?

import com.hotels.styx.api.HttpResponse;

/**
* Marker interface for classes that perform some modification
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Marker interface for classes that perform some modification
* An interface for classes that perform some modification

Marker interfaces are interfaces with no methods that just sort-of "mark" classes in some way (basically an old workaround before annotations were added to Java).

public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {

chain.proceed(request)
.flatMap(response -> it.aggregate(10000))
Copy link
Contributor

Choose a reason for hiding this comment

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

This code has syntax errors and does not compile.

@VivianLopes VivianLopes requested a review from mikkokar May 30, 2019 10:16
@@ -1,205 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we removed the 'parent' here so the pom file can be copied to real plugins that will not declare themselves as 'children' of styx-parent. Do we definitely need that?

@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
if (request.header("X-Respond").isPresent()) {
return Eventual.of(HttpResponse.response(OK)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consume the request body here?

import static java.util.Objects.requireNonNull;

/**
* You can replace without aggregating it first, if the replacement does not depend on the original contents.
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the beginning a bit unclear. A suggestion you can quickly discard : (The) body content can be replaced without ... Something along those lines.

</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned, all these changes in the dependencies mean that the pom file cannot be just copied to a new plugin. At some point we should make sure our BOM file does not expose all the dependencies of Styx and then have plugins import it...

@kvosper
Copy link
Contributor

kvosper commented Jun 12, 2019

See #432

@kvosper kvosper closed this Jun 12, 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
Development

Successfully merging this pull request may close these issues.

3 participants