Skip to content

Commit 0835118

Browse files
Egima profilepivovarit
authored andcommitted
added play-framework project (eugenp#678)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article
1 parent 334e1b3 commit 0835118

32 files changed

Lines changed: 1771 additions & 0 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
logs
2+
target
3+
/.idea
4+
/.idea_modules
5+
/.classpath
6+
/.project
7+
/.settings
8+
/RUNNING_PID

‎play-framework/student-api/LICENSE‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This software is licensed under the Apache 2 license, quoted below.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with
4+
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
5+
6+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
7+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
8+
language governing permissions and limitations under the License.

‎play-framework/student-api/README‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
This is your new Play application
2+
=================================
3+
4+
This file will be packaged with your application when using `activator dist`.
5+
6+
There are several demonstration files available in this template.
7+
8+
Controllers
9+
===========
10+
11+
- HomeController.java:
12+
13+
Shows how to handle simple HTTP requests.
14+
15+
- AsyncController.java:
16+
17+
Shows how to do asynchronous programming when handling a request.
18+
19+
- CountController.java:
20+
21+
Shows how to inject a component into a controller and use the component when
22+
handling requests.
23+
24+
Components
25+
==========
26+
27+
- Module.java:
28+
29+
Shows how to use Guice to bind all the components needed by your application.
30+
31+
- Counter.java:
32+
33+
An example of a component that contains state, in this case a simple counter.
34+
35+
- ApplicationTimer.java:
36+
37+
An example of a component that starts when the application starts and stops
38+
when the application stops.
39+
40+
Filters
41+
=======
42+
43+
- Filters.java:
44+
45+
Creates the list of HTTP filters used by your application.
46+
47+
- ExampleFilter.java
48+
49+
A simple filter that adds a header to every response.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import javax.inject.*;
2+
import play.*;
3+
import play.mvc.EssentialFilter;
4+
import play.http.HttpFilters;
5+
import play.mvc.*;
6+
7+
import filters.ExampleFilter;
8+
9+
/**
10+
* This class configures filters that run on every request. This
11+
* class is queried by Play to get a list of filters.
12+
*
13+
* Play will automatically use filters from any class called
14+
* <code>Filters</code> that is placed the root package. You can load filters
15+
* from a different class by adding a `play.http.filters` setting to
16+
* the <code>application.conf</code> configuration file.
17+
*/
18+
@Singleton
19+
public class Filters implements HttpFilters {
20+
21+
private final Environment env;
22+
private final EssentialFilter exampleFilter;
23+
24+
/**
25+
* @param env Basic environment settings for the current application.
26+
* @param exampleFilter A demonstration filter that adds a header to
27+
*/
28+
@Inject
29+
public Filters(Environment env, ExampleFilter exampleFilter) {
30+
this.env = env;
31+
this.exampleFilter = exampleFilter;
32+
}
33+
34+
@Override
35+
public EssentialFilter[] filters() {
36+
// Use the example filter if we're running development mode. If
37+
// we're running in production or test mode then don't use any
38+
// filters at all.
39+
if (env.mode().equals(Mode.DEV)) {
40+
return new EssentialFilter[] { exampleFilter };
41+
} else {
42+
return new EssentialFilter[] {};
43+
}
44+
}
45+
46+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.google.inject.AbstractModule;
2+
import java.time.Clock;
3+
4+
import services.ApplicationTimer;
5+
import services.AtomicCounter;
6+
import services.Counter;
7+
8+
/**
9+
* This class is a Guice module that tells Guice how to bind several
10+
* different types. This Guice module is created when the Play
11+
* application starts.
12+
*
13+
* Play will automatically use any class called `Module` that is in
14+
* the root package. You can create modules in other locations by
15+
* adding `play.modules.enabled` settings to the `application.conf`
16+
* configuration file.
17+
*/
18+
public class Module extends AbstractModule {
19+
20+
@Override
21+
public void configure() {
22+
// Use the system clock as the default implementation of Clock
23+
bind(Clock.class).toInstance(Clock.systemDefaultZone());
24+
// Ask Guice to create an instance of ApplicationTimer when the
25+
// application starts.
26+
bind(ApplicationTimer.class).asEagerSingleton();
27+
// Set AtomicCounter as the implementation for Counter.
28+
bind(Counter.class).to(AtomicCounter.class);
29+
}
30+
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package controllers;
2+
3+
import akka.actor.ActorSystem;
4+
import javax.inject.*;
5+
import play.*;
6+
import play.mvc.*;
7+
import java.util.concurrent.Executor;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.CompletionStage;
10+
import java.util.concurrent.TimeUnit;
11+
import scala.concurrent.duration.Duration;
12+
import scala.concurrent.ExecutionContextExecutor;
13+
14+
/**
15+
* This controller contains an action that demonstrates how to write
16+
* simple asynchronous code in a controller. It uses a timer to
17+
* asynchronously delay sending a response for 1 second.
18+
*
19+
* @param actorSystem We need the {@link ActorSystem}'s
20+
* {@link Scheduler} to run code after a delay.
21+
* @param exec We need a Java {@link Executor} to apply the result
22+
* of the {@link CompletableFuture} and a Scala
23+
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
24+
* An {@link ExecutionContextExecutor} implements both interfaces.
25+
*/
26+
@Singleton
27+
public class AsyncController extends Controller {
28+
29+
private final ActorSystem actorSystem;
30+
private final ExecutionContextExecutor exec;
31+
32+
@Inject
33+
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
34+
this.actorSystem = actorSystem;
35+
this.exec = exec;
36+
}
37+
38+
/**
39+
* An action that returns a plain text message after a delay
40+
* of 1 second.
41+
*
42+
* The configuration in the <code>routes</code> file means that this method
43+
* will be called when the application receives a <code>GET</code> request with
44+
* a path of <code>/message</code>.
45+
*/
46+
public CompletionStage<Result> message() {
47+
return getFutureMessage(1, TimeUnit.SECONDS).thenApplyAsync(Results::ok, exec);
48+
}
49+
50+
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
51+
CompletableFuture<String> future = new CompletableFuture<>();
52+
actorSystem.scheduler().scheduleOnce(
53+
Duration.create(time, timeUnit),
54+
() -> future.complete("Hi!"),
55+
exec
56+
);
57+
return future;
58+
}
59+
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package controllers;
2+
3+
import javax.inject.*;
4+
import play.*;
5+
import play.mvc.*;
6+
7+
import services.Counter;
8+
9+
/**
10+
* This controller demonstrates how to use dependency injection to
11+
* bind a component into a controller class. The class contains an
12+
* action that shows an incrementing count to users. The {@link Counter}
13+
* object is injected by the Guice dependency injection system.
14+
*/
15+
@Singleton
16+
public class CountController extends Controller {
17+
18+
private final Counter counter;
19+
20+
@Inject
21+
public CountController(Counter counter) {
22+
this.counter = counter;
23+
}
24+
25+
/**
26+
* An action that responds with the {@link Counter}'s current
27+
* count. The result is plain text. This action is mapped to
28+
* <code>GET</code> requests with a path of <code>/count</code>
29+
* requests by an entry in the <code>routes</code> config file.
30+
*/
31+
public Result count() {
32+
return ok(Integer.toString(counter.nextCount()));
33+
}
34+
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package controllers;
2+
3+
import play.mvc.*;
4+
5+
import views.html.*;
6+
7+
/**
8+
* This controller contains an action to handle HTTP requests
9+
* to the application's home page.
10+
*/
11+
public class HomeController extends Controller {
12+
13+
/**
14+
* An action that renders an HTML page with a welcome message.
15+
* The configuration in the <code>routes</code> file means that
16+
* this method will be called when the application receives a
17+
* <code>GET</code> request with a path of <code>/</code>.
18+
*/
19+
public Result index() {
20+
return ok(index.render("Your new application is ready."));
21+
}
22+
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package controllers;
2+
import models.*;
3+
import util.*;
4+
import play.mvc.*;
5+
import play.libs.Json;
6+
import play.libs.Json.*;
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import java.util.List;
10+
11+
public class StudentController extends Controller {
12+
public Result create() {
13+
JsonNode json = request().body().asJson();
14+
if(json == null)
15+
return badRequest(Util.createResponse("Expecting Json data",false));
16+
Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class));
17+
JsonNode jsonObject=Json.toJson(student);
18+
return created(Util.createResponse(jsonObject,true));
19+
}
20+
public Result update() {
21+
JsonNode json = request().body().asJson();
22+
if(json == null)
23+
return badRequest(Util.createResponse("Expecting Json data",false));
24+
Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
25+
if(student==null){
26+
return notFound(Util.createResponse("Student not found",false));
27+
}
28+
29+
JsonNode jsonObject=Json.toJson(student);
30+
return ok(Util.createResponse(jsonObject,true));
31+
}
32+
public Result retrieve(int id) {
33+
Student student=StudentStore.getInstance().getStudent(id);
34+
if(student==null){
35+
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
36+
}
37+
JsonNode jsonObjects=Json.toJson(student);
38+
return ok(Util.createResponse(jsonObjects,true));
39+
}
40+
public Result listStudents() {
41+
List<Student> result=StudentStore.getInstance().getAllStudents();
42+
ObjectMapper mapper = new ObjectMapper();
43+
44+
JsonNode jsonData=mapper.convertValue(result, JsonNode.class);
45+
return ok(Util.createResponse(jsonData,true));
46+
47+
}
48+
public Result delete(int id) {
49+
boolean status=StudentStore.getInstance().deleteStudent(id);
50+
if(!status){
51+
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
52+
}
53+
return ok(Util.createResponse("Student with id:"+id+" deleted",true));
54+
}
55+
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package filters;
2+
3+
import akka.stream.Materializer;
4+
import java.util.concurrent.CompletionStage;
5+
import java.util.concurrent.Executor;
6+
import java.util.function.Function;
7+
import javax.inject.*;
8+
import play.mvc.*;
9+
import play.mvc.Http.RequestHeader;
10+
11+
12+
/**
13+
* This is a simple filter that adds a header to all requests. It's
14+
* added to the application's list of filters by the
15+
* {@link Filters} class.
16+
*/
17+
@Singleton
18+
public class ExampleFilter extends Filter {
19+
20+
private final Executor exec;
21+
22+
/**
23+
* @param mat This object is needed to handle streaming of requests
24+
* and responses.
25+
* @param exec This class is needed to execute code asynchronously.
26+
* It is used below by the <code>thenAsyncApply</code> method.
27+
*/
28+
@Inject
29+
public ExampleFilter(Materializer mat, Executor exec) {
30+
super(mat);
31+
this.exec = exec;
32+
}
33+
34+
@Override
35+
public CompletionStage<Result> apply(
36+
Function<RequestHeader, CompletionStage<Result>> next,
37+
RequestHeader requestHeader) {
38+
39+
return next.apply(requestHeader).thenApplyAsync(
40+
result -> result.withHeader("X-ExampleFilter", "foo"),
41+
exec
42+
);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)