Bonsoir,
Je suis débutant en JavaEE et je n'arrive pas à récupérer ma ressource (Error 404).
Je m'excuse pour tout le code qui suit mais je suis totalement perdu ..
Classe ContextListener
Classe CountriesUtil
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 public class ContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub BasicConfigurator.configure(); CountriesUtil.init(sce.getServletContext()); } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub } }
Classe E212
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class CountriesUtil { private static final Logger logger = Logger.getLogger(CountriesUtil.class); private static final JSONArray ja = new JSONArray(); private static final Hashtable<String, String> name = new Hashtable<String, String>(); private static final Hashtable<String, String> alpha2 = new Hashtable<String, String>(); private static final Hashtable<String, String> alpha3 = new Hashtable<String, String>(); private static final Hashtable<String, String> e212 = new Hashtable<String, String>(); private static final Hashtable<String, String> latlon = new Hashtable<String, String>(); private static int size = 5; private static int nbCountries; private static String encode(String content) { Charset cs = Charset.forName("UTF-8"); ByteBuffer bb = ByteBuffer.wrap(content.getBytes()); CharBuffer cb = cs.decode(bb); String s = cb.toString(); return s; } private static void set(String key, JSONObject jo) { if (alpha2.containsKey(key)) logger.info("key: " + key + " existe déjà"); alpha2.put(key, "{\"ISO3166_1_Alpha_2\":\"" + (String) jo.get("ISO3166_1_Alpha_2") + "\"}"); alpha3.put(key, "{\"ISO3166_1_Alpha_3\":\"" + (String) jo.get("ISO3166_1_Alpha_3") + "\"}"); name.put(key, "{\"name\":\"" + (String) jo.get("name") + "\"}"); e212.put(key, "{\"ITU_E212\":\"" + (String) jo.get("ITU_E212") + "\"}"); latlon.put(key, "{\"lat\":\"" + (String) jo.get("lat") + "\",\"lng\":\"" + (String) jo.get("lng") + "\"}"); } @SuppressWarnings("unchecked") public static void init(ServletContext servletContext) { try { JSONObject jo = new JSONObject(); InputStream ips = servletContext.getResourceAsStream("../countries.txt"); InputStreamReader ipsr = new InputStreamReader(ips); BufferedReader br = new BufferedReader(ipsr); String ligne; boolean first = true; String[] keys = null; String[] values = null; // create json while ((ligne = br.readLine()) != null) { ligne = encode(ligne); if (first) { first = false; keys = ligne.split(";"); for (int i = 0; i < keys.length; i++) logger.info(keys[i]); } else { values = ligne.split(";"); jo = new JSONObject(); for (int i = 0; i < keys.length; i++) { jo.put(keys[i], values[i]); } ja.add(jo); logger.info(((JSONAware) jo).toJSONString()); } } nbCountries = ja.size(); for (int i = 0; i < ja.size(); i++) { jo = (JSONObject) ja.get(i); String CC = (String) jo.get("CC"); String NDC = (String) jo.get("NDC"); if (!NDC.isEmpty()) { String[] NDCs = NDC.split(","); for (int j = 0; j < NDCs.length; j++) { String CCNDC = CC + NDCs[j]; logger.info("CCNDC: " + CCNDC); CCNDC = CCNDC.trim(); if (CCNDC.length() > size) size = CCNDC.length() + 1; set(CCNDC, jo); } } else { set(CC, jo); logger.info("CCNDC: " + CC); } } br.close(); } catch (Exception e) { logger.info(e.getMessage()); } } public static String getE212(String Number) { for (int i = size; i > 0; i--) { if (Number.length() >= i - 1) { String key = Number.substring(0, i - 1); logger.info("i: " + i + ", number: " + Number.length() + ", key: " + key); if (e212.containsKey(key)) return e212.get(key); } } return null; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Path("/e212") @Api(value = "/e212", description = "Get Mobile Code Country (E212) from the phone number") @Produces({ "application/json", "application/xml" }) public class E212 { private static final Logger logger = Logger.getLogger(E212.class); @GET @Path("/{param}") @ApiOperation(value = "return param", notes = "see ITU-E212", response = E212.class) @ApiImplicitParam(name = "param",value = "id", required = true, dataType = "string", paramType = "path") @ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Process error"), @ApiResponse(code = 204, message = "No content") }) @Produces(MediaType.TEXT_HTML) public String getSomething(@PathParam("param") String id) { logger.info("start"); if (logger.isDebugEnabled()) { logger.debug("Start getSomething"); logger.debug("data: '" + id + "'"); } String response = CountriesUtil.getE212(id); if (logger.isDebugEnabled()) { logger.debug("result: '" + response + "'"); logger.debug("End getSomething"); } return response; } }
web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0"> <display-name>countrycode</display-name> <listener> <listener-class>ContextListener</listener-class> </listener> <context-param> <param-name>resteasy.resources</param-name> <param-value> E212 </param-value> </context-param> <context-param> <param-name>resteasy.providers</param-name> <param-value></param-value> </context-param> <listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <servlet> <servlet-name>RestEasyServletAdaptor</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> <init-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/v1</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>RestEasyServletAdaptor</servlet-name> <url-pattern>/v1/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Bonne soirée à vous et merci.
Partager