Spring + DWR fie upload size restriction - spring

I have an application in which files can be uploaded into the application using MultiPartRequest or DWR.
I have the following configuration.
Web.xml
<servlet>
<servlet-name>nerp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>fileUploadMaxBytes</param-name>
<param-value>5000</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>nerp</servlet-name>
<url-pattern>/nerp/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>nerp</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
nerp-servlet.xml
<beans:bean id="multipartResolver"
class="com.resources.erp.util.ERPMultiPartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<beans:property name="maxUploadSize" value="100000"/>
</beans:bean>
ERPMultiPartResolver
public class ERPMultiPartResolver extends CommonsMultipartResolver {
#Override
public boolean isMultipart(final HttpServletRequest request) {
boolean isMultiPartBoolean = true;
final String urlFromBrowserWithAppName = String.valueOf(request.getRequestURI());
String urlFromBrowser = "";
if(urlFromBrowserWithAppName != null) {
urlFromBrowser = urlFromBrowserWithAppName.replaceAll(String.valueOf(request.getContextPath()),"");
}
if(urlFromBrowser.indexOf("dwr") > 0) {
isMultiPartBoolean = false;
}
else {
isMultiPartBoolean = super.isMultipart(request);
}
return isMultiPartBoolean;
}
}
My MultiPartRequest related upload is getting restricted to the size specified in maxUploadSize property of the ERPMultiPartResolver. But I am not able to restrict the size of the DWR file upload.
I tried using the param-name fileUploadMaxBytes in web.xml but the image size is not getting restricted.
Can someone please help me out on how to restrict the size of the Image through DWR upload when both Spring 3.2.1 and Dwr 3.0 are being used in the project?
I have checked the link

The answer for my above question is that instead of declaring the fileUploadMaxBytes in web.xml in the manner I did above, the declaration should be in the place where you configure your dwrContoller. i.e.
<dwr:controller id="dwrController" debug="true" >
<dwr:config-param name="fileUploadMaxBytes" value="1048"/>
</dwr:controller>
Now with the help of this, I am able to restrict the image size to 1048 bytes.
Earlier my dwrController configuration is as shown below
<dwr:controller id="dwrController" debug="true" />

Related

How to make swagger working with jersey2 and JAX-RS on tomcat 7

I am trying to use swagger in order to document my Rest APIs. I use following link to setup with jersey2 and JAX-RS on tomcat
https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5
But I could not access either /swagger.json or /api-docs. Its responding with 404.
What am I doing wrong? Is there a workable documentation? Please help....
Probably you can try to better boil down, which part of the documentation is not yet working in your case.
For me, I used Jersey 2.5.1, and swagger-jersey2-jaxrs_2.10 v. 1.3.4.
I went with the solution to initialize Swagger by a bootstrap class which I included in my web.xml.
bootstrap class
public class SwaggerBootstrap extends HttpServlet {
#Override public void init(ServletConfig servletConfig) {
try {
ServletContext sc = servletConfig.getServletContext();
//as of servlet api 2.5
String ctxPath = sc.getContextPath();
String apiversion = "your-api-version";
String hostname = "your-hostname";
ConfigFactory.config().setBasePath("http://"+hostname+":8080"+ctxPath);
ConfigFactory.config().setApiPath("http://"+hostname+":8080"+ctxPath);
ConfigFactory.config().setApiVersion(apiversion);
ConfigFactory.config().setSwaggerVersion(com.wordnik.swagger.core.SwaggerSpec.version());
System.out.println("Swagger:");
System.out.println("api hostname:"+hostname);
System.out.println("context path:"+ctxPath);
System.out.println("api-version:"+apiversion);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to configure swagger");
}
}
web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.wordnik.swagger.jersey.listing</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>SwaggerBootstrap</servlet-name>
<servlet-class>my.package.swagger.SwaggerBootstrap</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Hope that helps.

How to load the data in index page using spring mvc?

I tried couple of Tutorial in Spring-MVC to load the data in index page without using ajax call means before loading the index page I want to get the data from server and load the data into the index page.but did not get proper answer.
Finally after couple of try i got the answer.here is my code.
web.xml
<display-name>SpringTest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
My Controller
#Controller
public class WebController {
#Autowired
private EmployeeService empService;
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index() {
List<Employee> empList = empService.getAllEmployee();
Collections.sort(empList, new EmployeeSortById());
ModelAndView modelMap = new ModelAndView("index","employeeList", empList);
System.out.println("Calling controller");
return modelMap;
}
}
spring-context.xml
<context:component-scan base-package="com.app.controller,com.app.dao.impl,com.app.service.impl" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>

spring-ws: no endpoint mapping found

I made a simple web service but when I'am trying to test it on soapui its giving this error:
WARN : [Oct-11 12:56:38,081] ws.server.EndpointNotFound - No endpoint mapping found for [SaajSoapMessage {http://www.servesy.com/api/v1/service}signupRequest]
I do not have any idea what should I do to make it correct, I saw many questions regarding this problem but did not find any solution.
My spring-ws configuration are follows:
(apart from this configuration I also tried to make simple input output example and that
also shows same warning)
web.xml
<web-app
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/servesy-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>servesyservices</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servesyservices</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servesyservices</servlet-name>
<url-pattern>/endpoints/*</url-pattern>
</servlet-mapping>
</web-app>
servesy-config.xml
<beans
<context:component-scan base-package="com.servesy.webservices" />
<sws:annotation-driven />
<bean id="ServesyService" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition" lazy-init="true">
<property name="schemaCollection">
<bean class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
<property name="inline" value="true" />
<property name="xsds">
<list>
<value>schemas/ServesyServices.xsd</value>
</list>
</property>
</bean>
</property>
<property name="portTypeName" value="ServesyService"/>
<property name="serviceName" value="ServesyServices" />
<property name="locationUri" value="/endpoints"/>
</bean>
</beans>
Endpoint
#Endpoint
public class ServesyWebServiceEndpoint {
private static final String TARGET_NAMESPACE ="http://www.servesy.com/api/v1/service";
private ServesyWebService servesyservice_i;
#Autowired
public void setServesyWebService(ServesyWebService servesyservice_p)
{
this.servesyservice_i = servesyservice_p;
}
#PayloadRoot(localPart="SignupRequest", namespace=TARGET_NAMESPACE)
public #ResponsePayload SignupResponse response(SignupRequest signupRequest) {
SignupResponse signupResponse = new SignupResponse();
Signup signup = servesyservice_i.signupResponse( signupRequest.getMobileNumber(), signupRequest.getPassword(), signupRequest.getCustomerName(), signupRequest.getEmailId(), signupRequest.getPromoCode(), signupRequest.getDevice());
signupResponse.setSignup(signup);
return signupResponse;
}
#PayloadRoot(localPart="LoginRequest", namespace=TARGET_NAMESPACE)
public #ResponsePayload LoginResponse response(LoginRequest loginRequest) {
LoginResponse loginResponse = new LoginResponse();
String string = servesyservice_i.signinResponse( loginRequest.getEmailID(), loginRequest.getPassword(), loginRequest.getDevice());
loginResponse.setSessionId(string);
return loginResponse;
}
}
and my soupui gives this type of blank output:
The EndpointNotFoundException occurs when Spring-WS cannot find a suitable #Endpoint that can handle the incoming request.
In this case, the incoming message has namespace http://www.servesy.com/api/v1/service and local name signupRequest (as can be seen in the log). While your #PayloadRoot mapping does have the same namespace; it does not have the same local name, as it uses SignupRequest with a capital S. Chances are that if you change the uppercase S to a lower case s in the #PayloadRoot annotation, it will work.
context level parameters need to be reflected in MessageDispatcherServlet level also.
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> <br>
<init-param> <br>
<param-name>transformWsdlLocations</param-name> <br>
<param-value>true</param-value> <br>
</init-param> <br>
<init-param> <br>
<param-name>contextConfigLocation</param-name> <br>
<param-value>/WEB-INF/config/servesy-config.xml</param-value> <br>
</init-param> <br>
For what's it worth, I was facing this issue 2021-02-17 14:13:52.810 DEBUG 204429 --- [nio-8080-exec-7] o.s.w.soap.server.SoapMessageDispatcher : Endpoint mapping [org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping#7f98f516] has no mapping for request while following - https://spring.io/guides/gs/producing-web-service/- was that I somehow missed to annotate the the endpoint with #Endpoint public class CountryEndpoint
I have faced a similar issue and the root cause was in the application servlet context path.
The best way how to approach this problem is, in my opinion, to set DEBUG logging level for "org.springframework.ws" and carefully watch logs. You should find a message like this:
o.s.w.s.e.mapping.UriEndpointMapping - Mapped key [/ws/CardInfoWS_v3] onto endpoint [bean 'cardInfoEndpointGateway_v3'; defined in: '...']
And the warn after sending request says:
o.s.w.s.e.mapping.UriEndpointMapping - Looking up endpoint for [/cms-ws/ws/CardInfoWS_v3]
o.s.ws.server.EndpointNotFound - No endpoint mapping found for [SaajSoapMessage {...}GetCardRequest]
So... it is looking for endpoint mapping under the key "/cms-ws/ws/CardInfoWS_v3" but the endpoint is registered under the key "/ws/CardInfoWS_v3".
You can also put the breakpoint to the method lookupEndpoint in your endpoint mapper (AbstractMapBasedEndpointMapping or AbstractMethodEndpointMapping) that looks like this:
protected Object lookupEndpoint(String key) {
return endpointMap.get(key);
}
and after sending the request, in the debug mode you will see all your registered endpoints and the key which is currently searched.

jersey 2.5 and Spring 2.5 integration not working

I am trying to integrate Jersey (Version 2.x) into our current project which use's Spring 2.5. I have followed all the steps mentioned in their Spring webapp integration sample, but can't seem to get the auto wiring to work when the Jersey bean is called.
My config/class files are given below,
WEB.XML FILE
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.inrev.rest.XXXSpringIntegration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
SPRING FILE
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean name="myresource" class="com.xxx.rest.MyResource" autowire="byName"></bean>
XXXSpringIntegration CLASS
public XXXSpringIntegration()
{
register(RequestContextFilter.class);
register(MyResource.class);
System.err.println("I am getting registered");
}
MyResource.class
#Path("myresource")
public class MyResource {
#Autowired
private IRAdminDAO adminDAO;
public MyResource()
{
System.err.println("Getting ready now");
}
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* #return String that will be returned as a text/plain response.
*/
#Path("/list")
#GET
#Produces("application/json")
public String list()
{
System.err.println("Adming DAO "+adminDAO);
return "Got it!";
}
public void setAdminDAO(IRAdminDAO adminDAO)
{
System.err.println("Adming dao being set "+adminDAO);
this.adminDAO = adminDAO;
}
}
Added configuration for adminDAO
<bean id="adminDAO" class="com.xxx.bm.dao.impl.IRAdminDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
I am sure that the auto wiring works when Spring get's initialised, as the Syserr in the setter get's called when the file get's initialised by spring. However, when I make the API call, the adminDAO is null.
http://bm.com:8080/bm/rest/myresource/list
What could be causing this, I have tried all other permutation combinations but somehow the autowiring doesn't seem to be working when we make the API call.
Regards
Jersey work's fine with Spring 2.5, it's just a few jar's which were not present in my build. I am not sure if they would come with the maven build, but are certainly not present in the .zip download.
Also, there is no exceptions suggesting the jar is missing. jersey-spring3-2.11.jar being the star.

Restful web service based on CXF

everyone!
Something has confused me a lot. I make a Restful Web service based on CXF,but it does not work.
My web.xml is as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
and then is my applicationContext.xml:
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="handleRequest" class="test.cjm.HandleReuqst"/> <jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<ref bean="handleRequest"/>
</jaxrs:serviceBeans>
and my HandleRequest.java file presents below:
#Produces("application/json")
#Path("/test")
public class HandleRequest {
#GET
#Path("/Spring")
public TestVO testSpring(){
System.out.println("get users:");
TestVO tv = TestDAO.getPerson();
return tv;
}
}
The TestDAO.java file
private static Map<String,TestVO> testVOs;
static{
testVOs = new HashMap<String,TestVO>();
TestVO t1 = new TestVO();
t1.setId(1);
t1.setName("cjm");
testVOs.put("1", t1);
}
public static TestVO getPerson(){
return testVOs.get("1");
}
I deploy the program in tomcat named CXFSpring. I don't know where is wrong, Everytime I make a request to localhost:8080/CXFSpring/test/Spring, It just return a 404 status.
Could you tell me Where is wrong?
you CXF serving servlet is mapped to /service/*
try the following URL: localhost:8080/CXFSpring/service/test/Spring

Resources