How to publish two services with same path but different URLs on Mule 3.2 - servlets

My need is to publish two services with same path on mule, but different URL's. Like this
https://localhost:8443/etc/app/version1/Service
https://localhost:8443/etc/app/version2/Service
Im using servlet mapping on web.xml
<servlet-mapping>
<servlet-name>muleServlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
And tried to use two different connectors since the path attribute doesn't allow me to use "version1/Service" or "version2/Service"
<servlet:connector
name="conectorVersion1"
servletUrl="https://localhost:8443/etc/app/version1/">
</servlet:connector>
<servlet:connector
name="conectorVersion2"
servletUrl="https://localhost:8443/etc/app/version2/">
</servlet:connector>
And finally, the endpoints
<flow
name="FlowVersion1"
processingStrategy="synchronous">
<servlet:inbound-endpoint
connector-ref="conectorVersion1"
path="Service">
<-- processors, jaxws-service, interceptors etc.. -->
</servlet:inbound-endpoint>
</flow>
<flow
name="FlowVersion2"
processingStrategy="synchronous">
<servlet:inbound-endpoint
connector-ref="conectorVersion2"
path="Service">
<-- processors, jaxws-service, interceptors etc.. -->
</servlet:inbound-endpoint>
</flow>
But i got this exception:
[[/etc]] StandardWrapper.Throwable: java.lang.IllegalStateException:
There are at least 2 connectors matching protocol "servlet", so the connector to use must be
specified on the endpoint using the 'connector' property/attribute.
Connectors in your configuration that support "servlet" are: conectorVersion1, conectorVersion2,
Thanks in advance.

I don't think it's valid to declare two servlet connectors: there's only one servlet context so a single connector is enough. Actually, I never declare the Servlet connector, as the default configuration works just fine.
So with only the following configuration:
<flow name="FlowVersion1" processingStrategy="synchronous">
<servlet:inbound-endpoint
path="version1/Service" />
<set-payload value="version 1" />
</flow>
<flow name="FlowVersion2" processingStrategy="synchronous">
<servlet:inbound-endpoint
path="version2/Service" />
<set-payload value="version 2" />
</flow>
I'm able to deploy in a servlet container (Jetty) and I can hit /{context}/app/version1/Service and /{context}/app/version2/Service without problem.

Related

how to set https for java servlet application

I have created a java servlet web application. It's working properly with "http". Now I want to set "https" secured connection to the server. For that, I have configured the things properly in the tomcatserver/conf/server.xml as follows.
Connector port="8443" protocol="HTTP/1.1"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="conf\localhost.jks" keystorePass="xxxx"
clientAuth="false" sslProtocol="TLS" />
then I have configured the things on web.xml file also as follows.
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOnly</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
when I send a request through postman, I am given following error.
here I have attached the request header also.
how can I solve this?

Azure web app delete logs directory on swap

I have multiple ASP .NET MVC and Web Api apps hosted on Azure.
I use two differents stages for deployment : stage and PROD. Basically, I want to have two different folders for logs : logs-stage and logs-PROD. This is working well based on my web.config and properties set directly in Azure.
The issue is that each time I deploy, all the previous logs are deleted. How can I avoid that ?
My NLog config looks like the following :
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="File" name="f" fileName="${basedir}/logs-${appsetting:name=Version:default=DEV}/website.${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${logger} - ${message}" />
<target name="email-Errors" xsi:type="Mail"
smtpServer="smtp.sendgrid.net"
smtpPort="myPort"
enableSsl="false"
smtpUsername="myUsername"
smtpPassword="myPassword"
smtpAuthentication="Basic"
from="myEmail"
to="${appsetting:name=WEBSITE_EMAIL_DEVELOPERS:default=myEmail}"
subject="[${appsetting:name=Version:default=DEV}][WEB][${uppercase:${level}}]"
layout="${longdate} ${uppercase:${level}} ${logger} - ${message}"
html="false" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="f" />
<logger name="*" minlevel="Error" writeTo="email-Errors" />
</rules>
</nlog>
The issue is that each time I deploy, all the previous logs are deleted. How can I avoid that ?
According to your description, it seems that when you publish the WebApp then remove additional file in the Azure WebApp. If it is that case please have a try to uncheck the [Remove additional files as destination] option during publish the WebApp.
Updated:
From the blog, we could know that if we swap the slots, just the DNS pointer changes, so if you have the nlog in the product after swap then the Nlog file is in the staging slot that not deleted. The following the snippet from the blog.
In short, the Swap operation will exchange the website's content between 2 deployment slots.
Swapped and what is not but note that swap is not about copying the content of the website but more about swapping DNS pointers.

getRequestDispatcher(.).forward(req,res) throws java.io.FileNotFoundException

I have upgraded my Servlet from 2.4 to 3.0 and deployed my application on Websphere 8.5.5.8. Application Server starts properly.
When I try to access my home.jsp page in browser it throws:
Controller Main Error OG1000SRVE0190E: File not found: /servlet/com.platform7.affina.operations.servlet.ValidateLoginUser
When I try to debug, code hits my Main Controller Servlet (which is in same package) but inside Controller servlet class I am calling:
this.getServletContext().getRequestDispatcher("Servlet/com.platform7.affina.operations.servlet.ValidateLoginUser").forward(request, response);
Which throws:
FileNotFoundException for Servlet/com.platform7.affina.operations.servlet.ValidateLoginUser.
But ValidateLoginUser is in the same package and classes folder location!
Folder structure:
\NEED4SPEEDCell02\operations_1.ear\OperationsWeb.war\WEB-INF\classes\com\platform7\affina\operations\servlet
ControllerMain.class and ValidateLoginUser.class are in same servlet package.
my Web.xml file:
<servlet>
<servlet-name>servletMain</servlet-name>
<servlet-class>com.platform7.affina.operations.servlet.ControllerMain</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servletMain</servlet-name>
<url-pattern>/controllerMain</url-pattern>
</servlet-mapping>
So when I access my URL: it hits ControllerMain.class but inside this class I am calling another servlet which is not part of web.xml but is located in same package of ControllerMain.class.
When I print realpath: this.getServletContext().getRealPath("/"));
I get:
C:\WebSphere858\AppServer\profiles\AppSrv01\installedApps\NEED4SPEEDCell02\operations_1.ear\OperationsWeb.war
I tried using getNamedDispatcher(..) too but throws: null.
Same code works fine on Websphere 7 and even works on Websphere 8.5.5.5
Due to security reasons the default setting for com.ibm.ws.webcontainer.disallowServeServletsByClassname property has been changed.
Please Note:This APAR has changed the default value of the
WebContainer custom property
com.ibm.ws.webcontainer.disallowServeServletsByClassname from false to
true so that no security threat could occur. Prior to this change, it
was up to the developer to remember to change the custom property to
true before deploying into production.
Property Name:
com.ibm.ws.webcontainer.disallowServeServletsByClassname Description:
If set to true, disallows the use of serveServletsByClassnameEnabled
at the application server level, overriding any setting of
serveServletsByClassnameEnabled at the application level. This
property affects all applications. Values: true(default)/false
You will need to add that custom property to the Web Container and set it to false for serving servlets by class name.
But as BalusC suggested, you should add your servlet to web.xml in the form:
<servlet>
<servlet-name>servletMain</servlet-name>
<servlet-class>com.platform7.affina.operations.servlet.ValidateLoginUser</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servletMain</servlet-name>
<url-pattern>/validateLoginUser</url-pattern>
</servlet-mapping>
and change that forward to:
this.getServletContext().getRequestDispatcher("/validateLoginUser").forward(request, response);
And do the same with your other class from the same package.
You seem to be relying on the legacy InvokerServlet which is known to have major security holes. This was deprecated in Tomcat 5 and clones (WebSphere 4) and removed in Tomcat 7 and clones (WebSphere 6).
You're not supposed to use it anymore. Just map the servlet on a normal URL pattern and invoke it. Assuming that the servlet is mapped on an URL pattern of /validateLoginUser via #WebServlet("/validateLoginUser") annotation on the servlet class, or via <url-pattern>/validateLoginUser</url-pattern> in web.xml mapping on the servlet, then you can get a request dispatcher on it as below:
request.getRequestDispatcher("/validateLoginUser");
Or, just refactor shared code to a plain Java class with a method and invoke it the usual Java way. It's these days kind of weird to have shared validation logic tight coupled in a servlet.
See also:
How to invoke a servlet without mapping in web.xml?
To make above upgrade working, I did few other changes as below for future references.
Mainly, I have to change binding files for websphere.
Previously, I had two bindings ibm-web-bnd.xmi and ibm-web-ext.xmi
ibm-web-bnd.xmi
<?xml version="1.0" encoding="UTF-8"?>
<com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:com.ibm.ejs.models.base.bindings.webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1226331113121" virtualHostName="default_host">
<webapp href="WEB-INF/web.xml#WebApp"/>
<resRefBindings xmi:id="ResourceRefBinding_1226331113121" jndiName="AffinaDataSource_pma">
<bindingResourceRef href="WEB-INF/web.xml#ResourceRef_AffinaDataSource_pma"/>
</resRefBindings>
</com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding>
ibm-web-ext.xmi
<?xml version="1.0" encoding="UTF-8"?>
<com.ibm.ejs.models.base.extensions.webappext:WebAppExtension
xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:com.ibm.ejs.models.base.extensions.webappext="webappext.xmi"
xmi:id="WebAppExtension_1226331113121"
serveServletsByClassnameEnabled="true">
<webApp href="WEB-INF/web.xml#WebApp"/>
<jspAttributes xmi:id="JSPAttribute_1226331113121" name="reloadEnabled" value="true"/>
<jspAttributes xmi:id="JSPAttribute_1226331113122" name="reloadInterval" value="10"/>
</com.ibm.ejs.models.base.extensions.webappext:WebAppExtension>
So as per servlet3 and Websphere 8.5.5.8, I change to replace above two .xmi files with ibm-web-bnd.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd" version="1.0">
<virtual-host name="default_host"/>
<resource-ref name="AffinaDataSourceAlias_pma" binding-name="AffinaDataSource_pma"/>
</web-bnd>
and then while installing application on Websphere 8.5.5.8, it use to throw outofmemmory error, so to fix that I change below max memory parameter from 256m to 512m in wsadmin.bat
C:\WebSphere858\AppServer\bin\wsadmin.bat
set PERFJAVAOPTION=-Xms256m -Xmx512m -Xquickstart
Hope this helps.

Accessing static html file within Spring MVC

I have set up mvc:resources in my mvc--dispatcher-servlet.xml as below
<mvc:resources mapping="/resources/**" location="resources/" />
<mvc:resources mapping="/favicon.ico" location="resources/images/favicon.ico" />
<mvc:resources mapping="/maintenance" location="resources/html/maintenance.html" />
So far it only works for the first two location (resource mappings). I can only access the static html by supplying the full URI
http://mydomain/resources/html/maintenance.html
instead of
http://mydomain/maintenance
UPDATED
I added <http pattern="/maintenance" security="none" /> in my spring-security-context.xml and it gives me new error, HTTP Status 404 - The requested resource is not available.
What did I miss here ?
I just found out the issue is caused by this
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The easy way out for me was to change the maintenance URI to /maintenance.html, thus the request is not 'handled' by the servlet.
If there's anyone can point out a better solution which allows me to access it as /maintenance, I'll accept that as the answer.

No mapping found for HTTP request with URI: in a Spring MVC app [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I'm getting this error.
my web.xml has this
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
I have this in my web-application-config.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<bean name="/Scheduling.htm" class="com.web.SchedulingController"/>
my com.web.SchedulingController looks like this
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class SchedulingController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView("/jsp/Scheduling_main.jsp");
modelAndView.addObject("message","Hello World MVC!!");
return modelAndView;
}
}
When I hit this controller with the URL http://localhost:8080/project1/app/Scheduling.htm
The Scheduling_main.jsp gets displayed but the images are not displayed properly. Also the js and css file are not getting rendered.
I'm accessing the images like this
<img src="jquerylib/images/save_32x32.png" title="Save Appointment">
If I change the URL mapping in the servlet definition to *.htm, the images get displayed fine. Can you point out where I'm missing out.
Here is complete error message
WARN [PageNotFound] No mapping found for HTTP request with URI [/mavenproject1/app/jquerylib/images/save_32x32.png] in DispatcherServlet with name 'springweb'
Thanks a lot.
Ravi
I'm think it happens because you try get your image though servlet (mapped as /app/*) You need get static content without handle it with your servlet, for example set image source to
<img src="../jquerylib/images/save_32x32.png" title="Save Appointment">
then real URI of your image will be /mavenproject1/jquerylib/images/save_32x32.png, and it will returned by your tomcat itself as is, without any processing.
I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite.xml) to solve the problem
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/</from>
<to>/app/welcome</to>
</rule>
<rule>
<from>/scripts/**</from>
<to>/scripts/$1</to>
</rule>
<rule>
<from>/styles/**</from>
<to>/styles/$1</to>
</rule>
<rule>
<from>/images/**</from>
<to>/images/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
How to handle static content in Spring MVC?
Add this to springweb-servlet.xml
<mvc:default-servlet-handler/>
Below text is extracted from Spring reference
This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet.

Resources