summaryrefslogtreecommitdiffstats
path: root/warehouse/src/main/java/app/market/web/controller/SpringBaseController.java
blob: b85f1e655b723796f716d2fd8b7d5fbec34422d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.market.web.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import app.market.utils.datatable.DataTableMap;
import app.market.utils.json.JsonMapperUtils;
import app.market.utils.json.JsonResult;

/**
 * Base Controller
 *
 * @author Toyota
 */
public abstract class SpringBaseController implements HandlerExceptionResolver {

    private static Logger logger = LoggerFactory.getLogger( SpringBaseController.class );

    protected final static String MODEL_ERRORS = "modelErrors";

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        Map<String, Object> model = new HashMap<String, Object>();
        if ( ex instanceof IOException ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, "No permission to read the record of this application!" );
            return new ModelAndView( "other/500", model );

        } else if ( ex instanceof SQLException ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, "The database error occurred." );
            return new ModelAndView( "other/500", model );

        } else if ( ex instanceof HttpClientErrorException ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, ex.getMessage() );
            return new ModelAndView( PageMapping.LOGIN.toString(), model );

        } else if ( ex instanceof RuntimeException ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, ex.getMessage() );
            return new ModelAndView( "other/500", model );

        } else if ( ex instanceof NullPointerException ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, ex.getMessage() );
            return new ModelAndView( "other/500", model );

        } else if ( ex instanceof Exception ) {
            logger.info( ex.getMessage() );
            model.put( MODEL_ERRORS, "A server error occurred. Please contact the administrator!" );
           // model.put( MODEL_ERRORS, "Your session has time out, please log on again.");
            return new ModelAndView( "other/500", model );
        }
        return null;
    }

    protected String writeDataTableMap(JsonResult jr, String draw) {
        DataTableMap map = new DataTableMap( draw, null );
        map.setErrorMsg( jr.getStatus(), jr.getData().toString() );
        return JsonMapperUtils.writeValueAsString( map.getMapData() );
    }

    protected String writeErrorList(JsonResult jr, List<String> errorList) {
        errorList.add( jr.getData().toString() );
        return JsonMapperUtils.getJsonString( jr.getStatus(), null, errorList );
    }

    protected String writeErrorString(JsonResult jr) {
        return JsonMapperUtils.getJsonString( jr.getStatus(), null, jr.getData().toString() );
    }

}