Здравствуйте, учусь java (api в основном, язык то, конечно, си-стайл знаком), хотя в общем то программирую не первый год и это не первый мой язык ООП. Да вот только когда старший программист увидел этот код, полез сначала на потолок, потом с него упал, потом закатился под стол, корчась толи от боли, то ли от смеха, не знаю в общем. Вердикт был: зарефакторить абсолютно все. Каждую строчку буквально. Что тут такого страшного, скажите пожалуйста?
| Код | public class ViewPhoto extends HttpServlet { private void sendPhoto(byte[] photo, HttpServletResponse resp) throws IOException { resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("pragma", "no-cache"); String format = ImageIO.getImageReaders(ImageIO.createImageInputStream(new ByteArrayInputStream(photo))).next().getFormatName().toLowerCase(); String mimeType = "image/" + format; resp.setContentType(mimeType); resp.setContentLength(photo.length); OutputStream out = resp.getOutputStream(); out.write(photo); out.close(); }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int id = Integer.valueOf(req.getParameter("id")); String table = req.getParameter("writeTo"); String type = req.getParameter("type");
try { AddPetService service = BeanProvider.getBean(); if ("mzsr".equals(table)) { MzsrAddressee addressee = service.getMzsrAddressee(id); if ("small".equals(type) && addressee.getBossSmallPhoto() != null) { sendPhoto(addressee.getBossSmallPhoto(), resp); return; } else if ("big".equals(type) && addressee.getBossPhoto() != null) { sendPhoto(addressee.getBossPhoto(), resp); return; } } if ("kiosk".equals(table)) { KioskAddressee addressee = service.getKioskAddressee(id); if ("small".equals(type) && addressee.getBossSmallPhoto() != null) { sendPhoto(addressee.getBossSmallPhoto(), resp); return; } else if ("big".equals(type) && addressee.getBossPhoto() != null) { sendPhoto(addressee.getBossPhoto(), resp); return; } } } catch (NamingException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; }
getServletContext().getRequestDispatcher("/i/no_photo.jpg").forward(req, resp); } }
|
|