Get HttpServletRequest from vert.web.ext context.request() - servlets

I am probably asking a similar question as this
is it possible to translate ServerHttpRequest and HttpServletRequest each other
I have an old 3rd party jar lib (no source code) that intakes/expects request of type javax.servlet.http.HttpServletRequest;
read vert.x http apis are a wrapper over jax-rs libs (could be wrong)
What is the best way to get javax.servlet.http.HttpServletRequest from vert.x context.request()?
Before anyone asks: yes the 3rd party jar manipulates headers, but we dont want to rewrite the entire jar lib ; experimented if #Context works, but didnt work)
Many thanks in advance!
java.lang.ClassCastException: class io.vertx.ext.web.impl.HttpServerRequestWrapper cannot be cast to class javax.servlet.http.HttpServletRequest (io.vertx.ext.web.impl.HttpServerRequestWrapper and javax.servlet.http.HttpServletRequest are in unnamed module of loader

Related

No conduit initiator found for namespace http://cxf.apache.org/transports/http

We are using apache cxf 2.6.1 to call Restful API. Both Server and the client use cxf libraries. On the client side, we use cxf generated client. Everything works in dev environment but we recently saw this error in production environment. I googled and found nothing for this specific namespace. I am wondering if anybody else got this. I already verified that the class path has following 5 cxf libraries (both in dev and prod). Any help would be greatly appreciated.
List of cxf jars we have in class path:
/opt/mhs/lib/cxf-api-2.6.1.jar
/opt/mhs/lib/cxf-rt-bindings-xml-2.6.1.jar
/opt/mhs/lib/cxf-rt-core-2.6.1.jar
/opt/mhs/lib/cxf-rt-frontend-jaxrs-2.6.1.jar
/opt/mhs/lib/cxf-rt-transports-http-2.6.1.jar
Here is the error message. Its very generic and I am having hard time finding the root cause.
apache.cxf.phase.PhaseInterceptorChain - Interceptor for {http://rest.perfmon.kiva.com/}IPerfMonRestEntityAssociationService has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No conduit initiator was found for the namespace http://cxf.apache.org/transports/http.
at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:127)
at org.apache.cxf.endpoint.UpfrontConduitSelector.selectConduit(UpfrontConduitSelector.java:77)
at org.apache.cxf.message.ExchangeImpl.getConduit(ExchangeImpl.java:130)
at org.apache.cxf.interceptor.MessageSenderInterceptor.getConduit(MessageSenderInterceptor.java:71)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:526)
at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:205)
at com.sun.proxy.$Proxy60.addEntityAssociation(Unknown Source)

Setting up a basic scala program on a server

As an experiment in scala I want to set up a basic website with scala as a server side language. This would not carry out many interactive tasks - it would simply write and distribute HTML in response to HTTP requests.
I don't know much about how the piping of web servers works, but I've written lots of HTML, CSS, JS and PHP and I envisage this working much like the PHP back end in wordpress - the client sends an HTTP request for a page such as example.wordpress.com/2012/06/18/example_blog and the PHP on the server compiles an HTML web page and returns it to the user.
Although I'm open to suggestions, I'd rather not use a full blown framework such as Lift as I'm trying to build from the ground up. I'm only interested in the very basic task of taking an HTTP request as input and outputting an HTTP response.
Usually you'd take a Java EE server and implement a Servlet.
Here is one:
package myservlet
import javax.servlet.http._
class Servlet extends HttpServlet {
/** Servlet's main method. */
protected def welcome (request: HttpServletRequest, response: HttpServletResponse): Unit = {
response.getWriter.write ("hi")
}
override def doGet (request: HttpServletRequest, response: HttpServletResponse): Unit = welcome (request, response)
override def doPost (request: HttpServletRequest, response: HttpServletResponse): Unit = welcome (request, response)
}
Then you'd mention it in web.xml as usual:
<servlet><servlet-name>MyServlet</servlet-name>
<servlet-class>myservlet.Servlet</servlet-class></servlet>
<servlet-mapping><servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern></servlet-mapping>
Refer to any Java Servlet tutorial for details.
You may not want to deal with Java EE and the whole servlet thing, especially if you don't come from the "Java world".
There are some really lightweight HTTP toolkits in Scala like Blueeyes or Play mini but my favorite is definitely Unfiltered
EDIT: A more complete answer on this thread Scala framework for a Rest API Server?

Updating ant style path capture from spring mvc 3.0 to 3.1

In spring 3.0.x I have a handler method that works perfectly like:
#RequestMapping("/foo/{version:\\d+}/**")
public void handleVersionedResource(#PathVariable("version") Long version, HttpServletRequest request, HttpServletResponse response) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// rest of the code maps path and version to a real resource and serves
// content with extra long cache control / expires headers
}
In 3.0.x, ff the request url is /foo/12345/some/resource, then path = "some/resource" (the ** part of the request match).
However, in 3.1.x (tried with both 3.1.0.RELEASE and 3.1.1.RELEASE) path = to the full request path match, so "/foo/12345/some/resource" in my previous example.
So the question is, does anyone have the simplest way to replicate the behavior I was getting in 3.0.x? Also, any insight to why this behavior has changed? Was I doing it wrong in the first place?
Update:
repeat of Spring 3.1.RC1 and PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE - no good answer was given there either.
Spring 3.1.RC1 and PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE suggests that HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE is internal to Spring and should not be used in your application
Doing something similar to How to match a Spring #RequestMapping having a #pathVariable containing "/"? would probably be your best bet

OVerride a url mapping defined in a controller present in a jar

I am using spring-security-oauth2 jar file. During the oauth verification process it makes a call to "/oauth/token" and hence the method. I want to modify the method..
Is it possible if i can override the url mapping already present? or exlcude the class from jar..
Thanks
Harshit

Using Freemarker with Restlet 2.0 in a Java EE server

I'm a bit confused with what is written in the documentation(s) for Freemarker and Restlet's freemarker extension.
Here's the situation: The restlet engine serves an HTML representation of a resource (e.g. www.mysite.com/{user}/updates). The resource returned for this URI is an HTML page containing all the updates, that is created with a freemarker template. This application is hosted on a Glassfish v3 server
Question(s):
The freemarker configuration should only be loaded once as per the freemarker documentation:
/* You should do this ONLY ONCE in the whole application life-cycle:Create and adjust the configuration */
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(
new File("/where/you/store/templates"));
cfg.setObjectWrapper(new DefaultObjectWrapper());
What is the best place to do this in a Java EE app? I am thinking of having it as context-param in web.xml and using a ServletContextListener - but I'm not sure how to go about doing that.
As per freemarker's documentation we could also add a freemarkerservlet and map .ftl url-patterns to it. But this is already mapped by a Restlet servlet (i.e., the url-pattern of "/"). So having another one for *.ftl doesn't make sense (or does it?)
So the question is basically about how best to integrate with the 'configuration' of Freemarker so that it happens only once and what is the 'entry-point' for that piece of code (who calls it). Has anyone successfully used Freemarker + restlet in a Java EE environment? Any ideas?
Thanks!
This was a tricky question - indeed. Required me to go through the implementation of the source files in org.restlet.ext.Freemarker package - Phew!
Here's how you can do it
If you need to create your OWN Configuration Object, set the 'templateLoader' to use and then have TemplateRepresentation 'work' on it for rendering:
Configuration cfg = new Configuration();
ContextTemplateLoader loader = new ContextTemplateLoader(getContext(),"war:///WEB-INF");
cfg.setTemplateLoader(loader);
TemplateRepresentation rep = null;
Mail mail = new Mail(); //The data object you wish to populate - example from Restlet itself
mail.setStatus("received");
mail.setSubject("Message to self");
mail.setContent("Doh!");
mail.setAccountRef(new Reference(getReference(), "..").getTargetRef()
.toString());
Map<String, Object> data = new HashMap<String, Object>();
data.put("status", mail.getStatus());
data.put("subject", mail.getSubject());
data.put("content", mail.getContent());
data.put("accountRef", mail.getAccountRef());
rep = new TemplateRepresentation("Mail.ftl", cfg, data, MediaType.TEXT_HTML);
return rep;
If you are happy with the default and wish to use a class loader based way of loading the templates
//Load the FreeMarker template
Representation mailFtl = new ClientResource(
LocalReference.createClapReference(getClass().getPackage())
+ "/Mail.ftl").get();
//Wraps the bean with a FreeMarker representation
return new TemplateRepresentation(mailFtl, mail, MediaType.TEXT_HTML);
If you want to initialize the Configuration Object once and set the template by calling the setServletContextForTemplateLoading(...) method on the configuration object. You could always do this in a ServletContextListener
public class Config implements ServletContextListener {
private static Configuration cfg = new Configuration();
#Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
cfg.setServletContextForTemplateLoading(sc, "/WEB-INF");
}
public static Configuration getFMConfig()
{
return cfg;
}
}
And then call the static getFMConfig() and pass it to TemplateRepresentation as in 1
Things to note:
If you do get a protocol not supported Exception it'll be in case 2. Implies that the ServerResource doesn't know what protocol to use to access the file - It'll be the CLAP protocol of Restlet. You may have to set up the init-params for RestletServlet in the web.xml file and have CLAP as one of the param-values
The TemplateRepresentation has quite a few constructors - if you DON'T pass in a configuration object during instantiation (using another overloaded constructor), it will create a new Configuration() for you. So you don't have to do any configuration set up as in 2 (This may strike you as obvious but I assumed that you WOULD still need to set a configuration or it would 'pick it up from somewhere')
If you DO wish to have your OWN configuration setup you MUST pass it to one of the constructors
Have a look at the "war:///" string in the constructor of ContextTemplateLoader in 1. this is important No where is it mentioned what this baseUri reference should be, not even in the docs. I tried for quite a while before figuring it out that it should be "war:///" followed by the folder name where the templates are stored.
For case 2 you'll probably have to store the templates in the same package as the class file from where this code is accessed. If you see carefully you'll notice a LocalReference parameter as an argument to ClientResource saying that the resource is supposed to be locally present and thus you need to use the custom CLAP protocol (classLoader Access Protocol)
Personal Frustration point - why isn't all this even clarified in the documentation or ANYWHERE :)
Hope it helps someone who stumbles upon this post! Phew!

Resources