Axis SecureSocketFactory - Setting the constructor attributes - attributes

I have a customer SecureSocketFactory set to be used by Axis when making an https connection using the following property:
AxisProperties.setProperty("axis.socketSecureFactory",
"com.metavante.csp.model.manager.mobilepayments.MonitiseSSLSocketFactory");
When this class is instantiated by Axis, the constructor with a Hashtable (attributes) is called. I see the timeout attribute is set in this table. Is there anyway to set more values in this?
I would like to be able to configure the Socket Factory on a per-instance scenario instead of globally by using static or system properties.
Edit: I found out these attributes are actually the HttpSender (BasicHandler) options. I still am unable to set these dynamically though.

I figured out a way around the problem. In my code where I wanted to set the property I use:
serviceLocator.getEngine().setOption(USE_CERT_PROPERTY, new Boolean(true));
where getEngine returns the AxisEngine in use. Then in the socket factory I can:
Boolean useSMS = (Boolean) MessageContext.getCurrentContext().getProperty(OtherClass.USE_CERT_PROPERTY);
I could set the object to whatever, maybe I'll go with the certificate name I needed. Hope this helps someone.

You can retrieve the SocketFactory instance and then change or add attributes, if you are interested in modify SocketFactory behavior. But if you do this, you also should inject the HashTable attribute (with the timeout). I think there is not a final and pretty solution.
AxisProperties.setProperty("org.apache.axis.components.net.SecureSocketFactory", MyAxisSocketFactory.class.getName());
MyAxisSocketFactory factory = (MyAxisSocketFactory) SocketFactoryFactory.getFactory("https", myHashTableParams);
factory.setMyStuff();
After this code, the instance of SocketFactory will be created and configured, and ready to use in web services, or whatever ^_^

Related

NonProxyHosts usage with Groovy HttpBuilder

If I create my httpBuilder as shown below (assume that a proxyUsername IS set, so setCredentials is called), then calls to httpAddress-es that are passed in properly are routed through the proxy. However, the Application has some http calls that are within the local network. Can http.nonProxyHosts be used to work around this and bypass the Proxy? If so, how? Use System.setProperty? Or something on HttpBuilder?
HTTPBuilder httpBuilder = new HTTPBuilder(httpAddress)
httpBuilder.setProxy(webProxyHost, webProxyPort, webProxyProtocol)
if (proxyUsername) {
httpBuilder.client.getCredentialsProvider().setCredentials(
new AuthScope(webProxyHost, webProxyPort),
new UsernamePasswordCredentials(proxyUsername, proxyPassword))
}
}
In the code above, all of the various named elements (webProxyHost, etc) are declared as String and set accordingly.
In answer to the question in the above comment, our primary 'nonProxyHost' need was for 'localhost' which is there by default. Thus this ceased to be an issue. Did not ever really find out how to accomplish this as it is somewhat version-specific on HttpClient.
You can set the System property:
System.setProperty('http.nonProxyHosts', myNonProxyHosts)
However, if you call 'setProxy' on HttpBuilder, even if you call 'useSystemProperties' it will not. This is in their documentation, just not obvious!
Finally, you might be able to call:
httpBuilder.client.params.setParameter('http.nonProxyHosts', myNonProxyHosts)
But I do not know for sure if that is the property name and documentation of those properties is hard to find. Worse - those 'params' are deprecated - you are supposed to use the better 'config' classes, though once again finding comprehensive documentation on all the parameters for that is not the easiest! Wish I could have been of more help!

How to globally change the command timeout using ServiceStack.OrmLite

I have a reporting-based website using ServiceStack and OrmLite, using a SQL Server back-end. Due to the duration of some of the reports, I'd like to either globally, or selectively (via Service-derived class or some attribute) make queries that run reports have a longer CommandTimeout. I'd like to keep using the existing code to query data for the reports (Db.Select, Db.SqlList calls), so I won't have to write boiler-plate code in each reporting class & method.
I've tried using the AlwaysUseCommand, but that requires declaring a single command that has an open connection. I don't see how I can do that without opening the connection in my AppHost.Configure method and never closing it. It'd be nicer if it were a function pointer, but alas.
I've also looked at overriding the Db property of Service, but that just returns an open IDbConnection without letting me override command created.
It seems like my last option is to create a custom DbConnection class, like the MiniProfiler's ProfiledDbConnection, but I'd have to implement a dozen abstract methods. It seems like overkill in order to set one property on a DbCommand.
So, in ServiceStack.OrmLite, how do I globally modify the CommandTimeout of DbCommands that are created?
You can change the CommandTimeout globally with:
OrmLiteConfig.CommandTimeout = NewTimeoutInSeconds;
Scoped Timeout
You can also specify a Timeout for a particular db connection with:
using (var db = dbFactory.OpenDbConnection())
{
db.SetCommandTimeout(NewTimeoutInSeconds);
}

JsConfig<MyClass>.ExcludePropertyNames example, not working for me

Trying to exclude properties from a model from being included during serialization.
I am using the following syntax:
JsConfig<MyTestClass>.ExcludePropertyNames = new[] { "ShortDescription" };
Just after that I have the following:
return (from o in __someProvider.GetAll() select (new
{
o.Name,
o.ShortDescription
o.InsertDate
}).TranslateTo<MyTestClass>()).ToList()
However once result is returned from the method, it still contains "ShortDescription" field in the Json. Am I doing something wrong?
JsConfig<T>.ExcludePropertyNames appears to be checked only once for each type, in a static constructor for TypeConfig<T>. Thus, if you are configuring ExcludePropertyNames in your service class, just before returning your response, it might be too late -- the TypeConfig properties may already be set up and cached for MyTestClass. I was able to reproduce this.
A more reliable alternative is to move all of your JsConfig<T> configuration to your AppHost setup code.
If you really do need to do this in your service class, e.g. if you are only conditionally excluding property names, then an alternative approach would be to ensure that JsConfig.IncludeNullValues is false (I believe it is by default) and in your service code set ShortDescription to null when appropriate.

Kohana helper attribute

I have a question that keeps bothering me. Currently, I have started using Kohana 3.2 Framework. I've written a helper to handle some functionality - I have a number of methods, which are (as it should be) declared STATIC. But, all of these methods are somehow working with the database, so I need to load a model. Currently, every method has a non-static variable like this:
$comment = new Model_Comments;
$comment->addComment("abc");
OK, it seems to be working, but then I wanted to get rid of this redundancy by using class attribute to hold the instance of the model (with is class as well).
Something like this:
private static $comment; // Declaring attribute
self::$comment = new Model_Comment; // This is done within helper __constuct method
self::$comment->addComment("abc"); // And call it within the method.
But, I got failed with: Call to a member function addComment() on a non-object
Question is: is it possible to do it ? Maybe there are some other approaches ?
Sorry for a long story and, thanks in advice! :P
A static method cannot call a non-static method without operating on an instance of the class. So, what you're proposing won't work. There may be a way do accomplish something similar, but what about trying the following:
You could implement the singleton or factory pattern for your "helper" class. Then, you could create the model (as an attribute) as you instantiate/return the instance. With an actual instance of your "helper" class, you won't have to worry about the static scope issues.
In other words, you can create a helper-like class as a "normal" class in your application that, upon creation, always has the necessary model available.
I'd be happy to help further if this approach makes sense.
David

Checking for an attribute on a destination property inside a custom AutoMapper TypeConverter

I have a custom type converter that converts UTC DateTime properties to a company's local time (talked about here: Globally apply value resolver with AutoMapper).
I'd now like to only have this converter do its thing if the property on the view model is tagged with a custom DisplayInLocalTime attribute.
Inside the type converter, if I implement the raw ITypeConvert<TSource, TDestination> interface, I can check if the destination view model property being converted has the attribute:
public class LocalizedDateTimeConverter : ITypeConverter<DateTime, DateTime>
{
public DateTime Convert(ResolutionContext context)
{
var shouldConvert = context.Parent.DestinationType
.GetProperty(context.MemberName)
.GetCustomAttributes(false)[0].GetType() == typeof(DisplayInLocalTimeAttribute);
if (shouldConvert) {
// rest of the conversion logic...
}
}
}
So this code works just fine (obviously there's more error checking and variables in there for readability).
My questions:
Is this the correct way to go about this? I haven't found anything Googling around or spelunking through the AutoMapper code base.
How would I unit test this? I can set the parent destination type on the ResolutionContext being passed in with a bit of funkiness, but can't set the member name as all implementors of IMemberAccessor are internal to AutoMapper. This, and the fact that it's super ugly to setup, makes me this isn't really supported or I'm going about it all wrong.
I'm using the latest TeamCity build of AutoMapper, BTW.
Don't unit test this, use an integration test. Just write a mapping test that actually calls AutoMapper, verifying that whatever use case this type converter is there to support works from the outside.
As a general rule, unit tests on extension points of someone else's API don't have as much value to me. Instead, I try to go through the front door and make sure that I've configured the extension point correctly as well.

Resources