Route to upload file in Play Framework 2.10 - java

I upload files to /upload folder, then I want to directly access my files, like:
http://localhost/upload/xxx.jpg
when I add routes as below:
GET /upload/*file controllers.Assets.at(path="/upload", file)
It causes another error:
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
Then, after I change #routes.Assets.at("stylesheets/main.css") to #routes.Assets.at("stylesheets/", "main.css"), there is another error:
[MatchError: (stylesheets/,main.css) (of class scala.Tuple2)]
(path: #unchecked, file: #unchecked) match {
Can somebody help me with this route? Thanks.

finnal, I got answer from playframework website, it not very obvious to find..
http://www.playframework.com/documentation/2.0.4/Assets
from this page:
However, if you define two mappings for the Assets.at action, like this:
GET /javascripts/*file controllers.Assets.at(path="/public/javascripts", file)
GET /images/*file controllers.Assets.at(path="/public/images", file)
Then you will need to specify both parameters when using the reverse router:
<script src="#routes.Assets.at("/public/javascripts", "jquery.js")"></script>
<image src="#routes.Assets.at("/public/images", "logo.png")">
but this may not solve my problem yet, it turn out to appear the second error mention in the question.
Be Careful, check the path param, it must be the same as you described in routes file. as:
when I set:
GET /public/*file controllers.Assets.at(path="/public", file)
in the html file, I should write as below:
#routes.Assets.at("/public", "stylesheets/main.css")
besides, if you use another folders, like /upload, adding below code in project/Build.scala in play.Project is essential. thanks TizianoPiccardi
playAssetsDirectories <+= baseDirectory / "foo"

You should add this line in project/Build.scala:
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
playAssetsDirectories <+= baseDirectory / "upload"
)
More info:
https://github.com/playframework/Play20/wiki/Assets

Related

How to set JsonTemplateLayout eventTemplateUri outside of classpath in Log4j2

In Log4j2's JsonTemplateLayout, I have no problem using eventTemplateUri: "classpath:LayoutTemplate.jsson" with json file located in rc/main/resources.
However, I would need to keep this file outside of class path. so I would like to use file location something like he way we add log fileName: c:\...
But it's throwing an error when I use eventTemplateUri: "C:\ ....\LayoutTemplate.json"
rror message is " Could not create plugin of type class org.apache.logging.log4j.layout.template.json.JsonTemplateLa
yout for element JsonTemplateLayout: java.lang.RuntimeException: failed reading URI: C:...
Did I miss anything here?
is it possible to place this json file outside of class path?
Thanks!
All *Uri configuration knobs in JsonTemplateLayout expect a URI string. In your case, the URI scheme is missing. The eventTemplateUri should look like file:///C:/path/to/LayoutTemplate.json in your case. For internals, see how o.a.l.l.l.template.json.util.Uris.readUri(String,Charset) is implemented.

Spring Rest Docs snippet template is ignored

I am attempting to be able to create custom snippet template for Spring Rest Docs documentation purposes. I have been following the reference guide
My first problem I ran into was in IntelliJ when creating a .snippet file in src/test/resources/org/springframework/restdocs/templates/asciidoctor/path-parameters.snippet as instructed by the guide. IntelliJ registers it as a jShell snippet file which from a quick glance is not the same as a AsciiDoc snippet. So I went into Settings -> Editor -> File Types and changed jShell Snippet from *.snippet to *.snippetOld. I then created a file type called Snippet and put it's pattern as *.snippet. This now fixes the problem that the snippet template is read as a jShell file. So the .snippet template I created no longer gets compile/validation errors.
But now when I run my mockMvc test after deleting the previous existing adoc files. The path-perameters.adoc file which should had used my custom template, if not use the default template, is now not generated at all.
In my mockMvc test I have the following
//Given
RestDocumentationResultHandler document = makeDocument("name-of-method");
document.document(
pathParameters(//do path parameters)
requestParameters(
parameterWithName("Month").description("The month requested").attributes(
key("type").value("integer"), key("constraints").value("more than 0 & less than 13.")
),
parameterWithName("Year").description("The year requested").attributes(
key("type").value("integer"), key("constraints").value("more than 1970 and less than current year")
)
),
responseField(//Do response fields)
);
// When
mvc.perform(get(REQUEST_PATH, USERID)
.contentType(MediaType.APPLICATION_JSON)
.param("month", "8")
.param("year", "2018"))
// Then
.andExpect(status().isOk())
.andDo(document);
And my snippet template is the following:
|===
|Parameter|Description|Type|Constraints
|{{parameter}}
|{{description}}
|{{type}}
|{{constraints}}
Could somebody point out what I've done wrong/different from the reference guide and how I could fix it to have my template working?
I suspect the reason you did not get output is that you confused path-parameters with request-parameters. Your documentation config specifies request parameters, but you customized path-parameters template
Here is the complete list of snippet templates:
curl-request.snippet
http-request.snippet
http-response.snippet
httpie-request.snippet
links.snippet
path-parameters.snippet
request-body.snippet
request-fields.snippet
request-headers.snippet
request-parameters.snippet
request-part-body.snippet
request-part-fields.snippet
request-parts.snippet
response-body.snippet
response-fields.snippet
response-headers.snippet
Source:
https://github.com/spring-projects/spring-restdocs/tree/master/spring-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor

Play Framework Public Assets "WEB Handler Not Found"

The first route below works, but the second doesn't. I want to serve a onepage angular/sockets app from my /public directory, but when I try via 2 below, I get a "WEB Handler Not Found error.
1/
GET /assets/*file controllers.cm_admin.Assets.versioned(path="/public", file: Asset)
2/
GET /*file controllers.cm_admin.Assets.versioned(path="/public", file: Asset)
Just worked it out.
I had the following statement in my app/Global:
case 'admin' => admin.Routes.routes.lift(request)
and changed it to:
case _ => admin.Routes.routes.lift(request)

Maven. Nested properties filtering doesn't work

I have two property files:
#environment.properties
env = production
and second file is:
#commons.properties
production.port = 123
test.port = 567
Also,I have resource file which need be filtered by environment.properties file and commons.properties file and copied.
The resource-file contains:
${${env}.port}
So,I want to filter my resource file with first file and get:
${production.port}
and then I want to filter it with second filter file and get:
123
I use maven 3.2.5 and the resource-file isn't filtered at all.
I know that there's issue related with this problem:
https://jira.codehaus.org/browse/MRESOURCES-70 But it still unresolved.
So,my question is - is there any solution to resolve this problem? (actually,I think that resource-plugin should be modified for work with nested property filtering).
And second question - does exist any way to avoid this problem by refactoring,I mean any other architecture solution. Or, what would you do if you had same problem?

Play! Framework 2.0.3, i18n error, `=' expected but `-' found

I'm new in "Play! Framework" and I'm trying to do a i18n for pt-BR.
My message file is called 'Message.pt-BR' and i put the pt-BR lang in application.conf.
In Java I'm using something like this:
flash("success", Messages.get("logout.success"));
And in scala.html files I'm using this:
#Messages("logout")
In my Message.pt-BR file I've this:
logout=Sair
logout.success=Logout realizado com sucesso. Volte sempre!
When I compile the project I have no errors, but when I request some page a have this error:
`=' expected but `-' found
Messages.pt-BR
Someone can help me?
I think you have to rename your message file from Messages.pt-BR to Messages.pt_BR (with a lower '_').

Categories

Resources