Is there a way to detect what browser the request is made in the HttpModule?
Thanks.
public class TestModule : IHttpModule
{
public void Dispose() {
throw new NotImplementedException();
}
public void Init(HttpApplication context) {
context.Request.Browser....;
}
}
MSDN Reference
Related
I ran across this url which suggests that an Http Handler can be added(Example is in Spring 1.x). https://lists.jboss.org/pipermail/undertow-dev/2017-March/001938.html
I have tried adding the following code - it does not appear to be called unless I add a listener. Unfortunately, Spring appears to have already added a listener. What would like to do is updates Spring's listener with my Http Handler. I am just not sure how to do it.
Any help is very much appreciated.
#Component
#Slf4j
public class LibCoreEmbeddedServletCustomerizer implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
#Value("${same.site.string}")
private String sameSiteString;
#Value("${server.port}")
private int serverPort;
#Value("${server.address}")
private String serverAddress;
#Override
public void customize(UndertowServletWebServerFactory factory) {
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
#Override
public void customize(Undertow.Builder builder) {
log.debug("LibCoreEmbeddedServletCustomerizer::customize");
UndertowBuilderCustomizer customizer = new UndertowBuilderCustomizer() {
#Override
public void customize(Undertow.Builder builder) {
builder.
//addHttpListener(serverPort, serverAddress)
setHandler(new HttpHandler() {
#Override
public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {
Map<String, Cookie> cookies = httpServerExchange.getResponseCookies();
log.debug(Encode.log(String.format("UndertowServletWebServerFactory handleRequest sameSiteString=%s", sameSiteString)));
for (Cookie cookie:cookies.values()) {
log.debug(Encode.log(String.format("UndertowServletWebServerFactory handleRequest cookie=%s", cookie)));
cookie.setSameSiteMode(sameSiteString);
}
}
});
}
};
factory.addBuilderCustomizers(customizer);
}
});
}
}
Try this:
SameSiteHandler goes through all of response cookies and moves SameSite information from Comment To SameSiteMode property.
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.Cookie;
import lombok.RequiredArgsConstructor;
#RequiredArgsConstructor
public class SameSiteHandler implements HttpHandler {
private final HttpHandler nextHttpHandler;
#Override
public void handleRequest(HttpServerExchange httpHandlerExchange) throws Exception {
httpHandlerExchange.addResponseCommitListener(exchange -> {
var cookies = exchange.getResponseCookies();
if (cookies != null) {
cookies.forEach((name, cookie) -> fix(cookie)));
}
});
nextHttpHandler.handleRequest(httpHandlerExchange);
}
/** Moves SameSite value from Comment to SameSiteMode */
private void fix(Cookie cookie) {
if (cookie == null) {
return;
}
var comment = cookie.getComment();
cookie.setComment(null);
cookie.setSameSiteMode(comment);
}
}
Register SameSiteHandler
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;
#Configuration
public class SameSiteHandlerConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
#Override
public void customize(UndertowServletWebServerFactory factory) {
factory.addDeploymentInfoCustomizers(deploymentInfo ->
deploymentInfo.addInitialHandlerChainWrapper(SameSiteHandler::new));
}
}
In MvvmCross 4.1.4 for Window Universal App (UWP) platform, if we call ShowViewModel too early within ViewModel (like in Constructor, Init, or Start event) then it does not navigate to another model.
public class FirstViewModel : MvxViewModel
{
public FirstViewModel()
{
ShowViewModel<SecondViewModel>();
}
}
Note that it works just fine for iOS and Android platform.
It is a bug of MvvmCross (according to this https://github.com/MvvmCross/MvvmCross/issues/1223).
The work around solution is to trigger the navigation from some events like View_Loaded or View_GotFocus within the View:
public sealed partial class FirstView : MvxWindowsPage
{
public FirstView()
{
this.InitializeComponent();
this.Loaded += FirstView_Loaded;
}
private void FirstView_Loaded(object sender, RoutedEventArgs e)
{
var viewModel = base.ViewModel as FirstViewModel
if (viewModel != null)
{
viewModel.Initialise();
}
}
}
ViewModel updated:
public class FirstViewModel : MvxViewModel
{
public FirstViewModel()
{
}
public void Initialise()
{
//Navigate here
ShowViewModel<SecondViewModel>();
}
}
When I add this code to my viewController in my Xamarin.iOS app:
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://test-database.azure-mobile.net/",
"FxMRoOZYMaYFrJDZEhVOkziHEwzkqw26"
);
I get this error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
How do I resolve this error?
Here is my whole ViewController:
using System;
using System.Drawing;
using Foundation;
using UIKit;
using Microsoft.WindowsAzure.MobileServices;
namespace MirrorMirror
{
public partial class RootViewController : UIViewController
{
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://test-database.azure-mobile.net/",
"FxMRoOZYMaYFrJDZEhVOkziHEwzkqw26"
);
//private readonly IMobileServiceTable<ToDoItem> ToDoTable = MobileService.GetTable<ToDoItem>();
static bool UserInterfaceIdiomIsPhone
{
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
}
public RootViewController(IntPtr handle) : base(handle)
{
}
public override void DidReceiveMemoryWarning()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
#region View lifecycle
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
}
#endregion
partial void RefreshButton_TouchUpInside(UIButton sender)
{
var ToDo = new ToDoItem();
ToDo.Text = "Buy Milk";
// ToDoTable.InsertAsync(ToDo);
}
}
}
I need to share an attribute between the beforePhase() and the afterPhase() methods of my PhaseListener, for a same JSF request.
Is the following snippet thread-safe?
public class MyPhaseListener implements PhaseListener {
private MyObject o = null;
#Override
public void beforePhase(PhaseEvent event) {
if (condition) {
o = new MyObject();
}
}
#Override
public void afterPhase(PhaseEvent event) {
if (o != null) {
o.process();
o = null;
}
}
#Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
If not, what are other solutions?
This is definitely not threadsafe. There's only one phase listener instance applicationwide which is shared across multiple requests. Basically, a phase listener is like an #ApplicationScoped managed bean.
Just set it as a context attribute.
public class MyPhaseListener implements PhaseListener {
#Override
public void beforePhase(PhaseEvent event) {
if (condition) {
event.getFacesContext().setAttribute("o", new MyObject());
}
}
#Override
public void afterPhase(PhaseEvent event) {
MyObject o = (MyObject) event.getFacesContext().getAttribute("o");
if (o != null) {
o.process();
}
}
#Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
You could use ThreadLocal for this, but it tends to have issues in environments having different classloaders, to name it: memory leak. Be sure to check for that in the given environment...
Also, you should make it sure that if the processing can be interrupted (e.g. exception...) between the beforePhase() and afterPhase() methods, the ThreadLocal should be handled appropriately...
This is what it would look like:
public class MyPhaseListener implements PhaseListener {
//if null is a valid value, no initial setting is needed
private ThreadLocal<Object> myStateObject = new ThreadLocal<Object> ();
#Override
public void beforePhase(PhaseEvent event) {
//might be needed, to guarrantee no residue from an aborted processing is in there
myState.set(null);
if (condition) {
myState.set(<Object representing the state>);
}
}
#Override
public void afterPhase(PhaseEvent event) {
try {
Object stateObject = myState.get();
if (stateObejct!=null) {
//do what you have to
}
} finally {
//to be sure
myState.remove();
}
}
}
In this article the author uses ThreadLocal too...
Also, this article is also a great eye-opener, explaining why not to share mutable instance-level information:
One thing to remember though, is that PhaseListener instances are application-wide Singletons that are referenced by the JSF Lifecycle, which itself is an application-wide Singleton.
EDIT just saw Boolean got updated to Object, adjusted example
I am trying to re-use the service registrations in an assembly that I use through a few services in my solution. I follow the example listed from the NServiceBus website to implement the solution. When following that, unless I add the IWantCustomInitialization interface, my Init method (and IoC container implementation) appears not to function. When I have that interface implemented, I get exceptions (listed in SO questions here and here). I can't seem to get it to work that there are no exceptions AND the dependencies in my MessageHandler are being populated properly. Here is my current EndpointConfig implementation.
[EndpointSLA("00:00:30")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, UsingTransport<Msmq>, INeedInitialization {
public void Init() {
Configure.With().ObjectBuilderAdapter();
}
}
public class ObjectBuilderAdapter : IContainer {
readonly IDependencyInjector injector;
public ObjectBuilderAdapter(IDependencyInjectionBuilder dependencyInjectionBuilder) {
injector = dependencyInjectionBuilder.Create(); //This method does all the common service registrations that I am trying to re-use
//injector.RegisterType<ExtractIncomingPrincipal, PrincipalExtractor>();
}
public void Dispose() {
injector.Dispose();
}
public object Build(Type typeToBuild) {
return injector.Resolve(typeToBuild);
}
public IContainer BuildChildContainer() {
return new ObjectBuilderAdapter(new DependencyInjectorBuilder());
}
public IEnumerable<object> BuildAll(Type typeToBuild) {
return injector.ResolveAll(typeToBuild);
}
public void Configure(Type component, DependencyLifecycle dependencyLifecycle) {
injector.RegisterType(component);
}
public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle) {
injector.RegisterType(component);
}
public void ConfigureProperty(Type component, string property, object value) {
if (injector is AutofacDependencyInjector) {
((AutofacDependencyInjector)injector).ConfigureProperty(component, property, value);
} else {
Debug.WriteLine("Configuring {0} for property {1} but we don't handle this scenario.", component.Name, property);
}
}
public void RegisterSingleton(Type lookupType, object instance) {
injector.RegisterInstance(lookupType, instance);
}
public bool HasComponent(Type componentType) {
return injector.IsRegistered(componentType);
}
public void Release(object instance) { }
}
public static class Extensions {
public static Configure ObjectBuilderAdapter(this Configure config) {
ConfigureCommon.With(config, new ObjectBuilderAdapter(new DependencyInjectorBuilder()));
return config;
}
}
Note: When I use the INeedInitialization interface, I get the ComponentNotRegisteredException when it's looking for IStartableBus.
When you are trying to swap the built in container, then you need to implement IWantCustomInitialization in the same class that implements IConfigureThisEndpoint.
You can use your own container and register all your types in there and tell NSB to use that container.
For example:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
public void Init()
{
var container = new ContainerBuilder().Build();
Configure.With()
.AutofacBuilder(container);
}
}