diff options
author | Abyss777 <abyss@fox5.ru> | 2018-02-02 11:41:54 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2018-02-02 11:41:54 +0500 |
commit | 16f912fa4ebfbb89a5d99116c12740229fa09b67 (patch) | |
tree | afa0cf6179617535cf3e404229c75641833a2d05 /src/org/traccar/api | |
parent | 195ec5aed9e3a6a499cf5a21773235563806a0c1 (diff) | |
download | trackermap-server-16f912fa4ebfbb89a5d99116c12740229fa09b67.tar.gz trackermap-server-16f912fa4ebfbb89a5d99116c12740229fa09b67.tar.bz2 trackermap-server-16f912fa4ebfbb89a5d99116c12740229fa09b67.zip |
Optimize exception handling
Diffstat (limited to 'src/org/traccar/api')
-rw-r--r-- | src/org/traccar/api/MediaFilter.java | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/org/traccar/api/MediaFilter.java b/src/org/traccar/api/MediaFilter.java index b3dcbbd66..de15209e7 100644 --- a/src/org/traccar/api/MediaFilter.java +++ b/src/org/traccar/api/MediaFilter.java @@ -17,6 +17,7 @@ package org.traccar.api; import java.io.IOException; +import java.sql.SQLException; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -27,7 +28,6 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import javax.ws.rs.NotAuthorizedException; import org.traccar.Context; import org.traccar.api.resource.SessionResource; @@ -54,7 +54,9 @@ public class MediaFilter implements Filter { } } if (userId == null) { - throw new NotAuthorizedException("Not authorized"); + ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + response.getWriter().println("Not authorized"); + return; } String path = ((HttpServletRequest) request).getPathInfo(); @@ -62,26 +64,22 @@ public class MediaFilter implements Filter { if (parts.length < 2 || parts.length == 2 && !path.endsWith("/")) { Context.getPermissionsManager().checkAdmin(userId); } else { - Device device = Context.getIdentityManager().getByUniqueId(parts[1]); + Device device = Context.getDeviceManager().getByUniqueId(parts[1]); if (device != null) { Context.getPermissionsManager().checkDevice(userId, device.getId()); } else { - throw new IllegalArgumentException("Device not found"); + ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_FOUND); + response.getWriter().println("Device not found"); + return; } } chain.doFilter(request, response); - } catch (Exception e) { - HttpServletResponse httpResponse = (HttpServletResponse) response; - if (e instanceof SecurityException) { - httpResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); - } else if (e instanceof IllegalArgumentException) { - httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); - } else if (e instanceof NotAuthorizedException) { - httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - } else { - httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); - } + } catch (SecurityException e) { + ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_FORBIDDEN); + response.getWriter().println(Log.exceptionStack(e)); + } catch (SQLException e) { + ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getWriter().println(Log.exceptionStack(e)); } } |