In a web app using Quarkus, RestEasy and Qute, I've successfully created a Qute template and message bundles for different languages. However, I haven't succeeded in having it select the language automatically based on the HTTP header Accept-Language.
The Qute References states:
When using quarkus-resteasy-qute
the locale attribute is derived from
the the Accept-Language header if not set by a user.
I'm using the quarkus-resteasy-qute extension (in pom.xml) and I can see that the Accept-Language header is set to Accept-Language: fr,en-US;q=0.7,en;q=0.3. But the page is displayed in English. So what am I missing? Is there some code I need to add?
If I explictly set the language to French (see below), it is shown in French. So the template is multi-language.
#Path("/test")
public class HomeController {
#Location("home")
Template homeTemplate;
#GET
#Produces(MediaType.TEXT_HTML)
public TemplateInstance showHome() {
return homeTemplate
.instance()
.setAttribute("locale", "fr"); // hard-coded language works
}
}
The resteasy-qute integration does not support this feature yet. Feel free to create a feature request here: https://github.com/quarkusio/quarkus/issues.
You can also try to send a PR instead ;-). This JAX-RS ContainerResponseFilter needs to be modified: https://github.com/quarkusio/quarkus/blob/main/extensions/resteasy-classic/resteasy-qute/runtime/src/main/java/io/quarkus/resteasy/qute/runtime/TemplateResponseFilter.java
Related
I'm facing some issues while trying to work with cookie in distincts domains. In my scenario I have two apps (app1 and app2). Basically, the app1 is doing an ajax request to the app2 in order to this one creates a cookie (i.e. dummyCookie). The app2 is an application running over the Play framework 2.5 for Java. I'm creating the cookie like this:
response().setCookie(Http.Cookie.builder("dummyCookie", "9e0a6b4c-58ed-b700-0000-015ec494956").build());
I'm using the plays's CORSFilter:
package myCustomFilters.filters;
import play.filters.cors.CORSFilter;
import play.http.DefaultHttpFilters;
import javax.inject.Inject;
public class Filters extends DefaultHttpFilters {
#Inject
public Filters(CORSFilter corsFilter) {
super(corsFilter);
}
}
In my application.conf I have this configuration:
play.http.filters = "myCustomFilters.Filters"
play.filters {
cors {
pathPrefixes = ["/"]
allowedOrigins = null
allowedHttpMethods = ["POST, GET, PUT, DELETE, OPTIONS"]
}
}
In the Chrome's console, in the Network tab, I could see the cookie in the response header.
If I check the Application tab, Cookies session, I couldn't see the cookie there:
I did some investigations and maybe the problem is related to the scenario "CORS + AJAX", since that I have one application calling (via ajax request) the another one to generate a cookie.
Guys, somebody already faced with this kind of scenario?
I had similar issues. I had to tweak the configuration in different ways to make that work in Chrome. And client request also need some specific changes (I use JQuery, and this kind of approach was necessary : Sending credentials with cross-domain posts?).
However, in the end, I discovered that Safari would most likely never work for me, as Apple now has specific rules to handle when cookies can be sent cross domain or not. Depending on what you are trying to achieve, this might lead you to consider a totally different approach.
I have created a web performance test in VS 2015, and am trying to set request headers. When setting headers such as "Referer" or "User-Agent" everything seems to work fine. I just set the header by right-clicking the request and adding a new header, and when I run the test and inspect the request I can see that the new header has been added. However if I try to add a new header named "Content-Length" and then run the test, this header is missing from the request. I'm getting an http error which I assume is because VS doesn't use the "Content-Length" header:
HTTP Error 411. The request must be chunked or have a content length.
I ran into this problem as well. The consistent trend with these issues online seems to be when the content-length is 0. Otherwise VS seems to dynamically come up with the content-length header on it's own.
Explanation of Problem
Esentially, I don't think you can add it manually. VS wants to add this itself, so the real problem is why isn't VS adding this?
The problem comes from the request being simply a "Request" and not a "Web Service Request", the significant difference here being that the latter includes the "String Body" component. I'm assuming that VS does this because the content-length it recorded was 0. I recorded my workflow in Fiddler and got the same result. I would also assume that the reason adding the Content-Length header doesn't work is because this gets calculated by Visual Studio if the request has a body in the request.
The Workaround
Make the request a "Web Service Request" so that it includes the String Body component. In the test, right click and choose "Insert Web Service Request".
I got clued into this from the below post (which also includes an example for a coded UI test):
https://social.msdn.microsoft.com/Forums/en-US/da492346-760e-4971-a666-8897ae7b35e3/length-required-error?forum=vstswebtest
Plugin Option
I couldn't find a way to just convert my existing request into a Web Service Request, so I created a WebTestRequestPlugin that can be added to a request to dynamically add a body to the request.
Add the below to your project
Change the namespace to match your project's name
Build your solution.
Right click on the problematic request and click "Add Request Plugin" and then select this plugin.
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.ComponentModel;
namespace YourProjectName
{
[DisplayName("Add Request Body")]
[Description("If a request is intended to have a content-length of 0, the Web Test won't include an empty String Body and the Content-Length header won't " +
"be added during test execution. This may result in a 411 - Length required error. This plugin can be added to that request to add a body to the request" +
"during test execution.")]
public class AddBodyToRequestPlugin : WebTestRequestPlugin
{
public AddBodyToRequestPlugin()
{
}
public override void PreRequest(object sender, PreRequestEventArgs e)
{
// Add string body if it doesn't exist yet.
if ( e.Request.Body == null )
{
e.Request.Body = new StringHttpBody();
}
}
}
}
I'm currently watching a video course about ASP.NET Web API. When a controller gets called, the data gets returned in JSON by default. I was just wondering, because when I copy this sample project from the video, I get XML.
The frustration is big, please help me to solve this.
I'm pretty new to ASP.NET Web API, so please bear with me.
UPDATE
The controller doesn't contain special code. It's the simple code, which gets generated from the API Controller with empty read/write actions template.
ASP.NET WebAPI comes with built-in content negotitation therefore the format of the return value is determined by the request itself - more specifically by the Accept/Content-Type headers (depending on which ones are present, Accept header appears to be favoured over the Content-type).
I assume you're viewing the results in a browser and by default it's probably asking for application/xml. You will need to toy around with some settings/developer tools on the browser and force it to send Content-Type: application/json to get the correct response (assuming your returning HttpResponseMessage).
in Global.asax: add the line:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
It'll look like this.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
James is close, but the content negotiation actually uses the [Accept] header,
not [Content-Type]
As with nearly everything else in MVC, you can override the content negotiation components to ensure the desire content is returned
W3c clearly states-
14.1 Accept
The Accept request-header field can be used to specify certain media types which are acceptable for the response.
-and-
14.17 Content-Type
The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the case of the HEAD method, the media type that would have been sent had the request been a GET.
This page headers is very useful to understand request/response negotiation.
Are there any possible security issues or pitfalls to, within a custom AuthorizeAttibute (registered globally), apply authorization based on the controller type and action being called?
e.g. (not real code)
string controllerFullName=_filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;
string minRequiredRole = GetControllerMinRequiredRole(controllerFullName);
if(User.MeetsRoleRequirement(minRequiredRole))
{
//give access
}
else
{
//no you're not allowed
}
The main issue is with Authorization caching - so there are a few things to know. Check out the links I've posted here:
Creating a AuthorizeAttribute - what do I need to know?
Look at the code to the existing attribute and how it handles caching to ensure you arent causing the same issue the base attribute prevents.
I am trying to optimize the performance of a Wicket 1.5.3 application.
I'm trying to get the caching configuration up and running. I've already reviewed "migration to 1.5" papers, the migration guide and samples. I've also ensured that there is a default caching strategy available, and tried to set a custom one.
getResourceSettings().setCachingStrategy(strat);
The app has CSS and JS in a Base-Frame.html header as link and script, and it has a lot of images. I'm currently using something like this:
Image img = new Image("logoutImg") {
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
tag.put("src", baseUrl + "/images/logout.png");
}
};
With that, the HTTP header output is always:
Pragma No-cache
Cache-Control no-cache
for all resources and pages.
I've now implemented some servlet filters, which is a rather brutish method that avoids all previously set Wicket headers.
Could anyone provide a running working example, or some tips for getting this up and running? In particular, something using FilenameWithVersionResourceCachingStrategy would be really helpful, since that seems to be a good solution.
I guess you have to use Wicket's CachingImage class allowing you to set the headers accordingly to the browser