Java tutorial
/* * Copyright (C) 2013 "Erhan Bagdemir" * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.bagdemir.eboda.handler; import static com.bagdemir.eboda.utils.HttpUtils.responseWith404; import static com.bagdemir.eboda.utils.HttpUtils.responseWith501; import static com.bagdemir.eboda.utils.HttpUtils.sendBody; import static com.bagdemir.eboda.utils.HttpUtils.sendHttpOKHeaders; import static com.bagdemir.eboda.utils.Statics.HTTP_CONTENT_TYPE; import static com.bagdemir.eboda.utils.Statics.HTTP_GET; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import com.bagdemir.eboda.resource.ByteArrayResource; import com.bagdemir.eboda.resource.Resource; import com.bagdemir.eboda.utils.HttpUtils; import com.bagdemir.eboda.utils.Statics; import com.google.common.collect.ImmutableMap; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; public class MemoryResourceHandler implements HttpHandler { public static final Logger LOGGER = Logger.getLogger(MemoryResourceHandler.class); public static final String MIME_IMAGE_GIF = "image/gif"; private Map<String, Resource> inMemoryResources = new HashMap<>(); private String[] gifImages = new String[] { "icons/folder.gif", "icons/back.gif", "icons/file.gif" }; public MemoryResourceHandler() { for (final String resourceName : gifImages) { readResourceIfExists(resourceName); } } private void readResourceIfExists(final String resourceName) { try { final byte[] bytes = IOUtils.toByteArray(getClass().getClassLoader().getResource(resourceName)); final ByteArrayResource byteArrayResource = new ByteArrayResource(bytes, MIME_IMAGE_GIF); inMemoryResources.put(resourceName, byteArrayResource); } catch (IOException e) { LOGGER.error(e); } } @Override public void handle(final HttpExchange httpExchange) throws IOException { LOGGER.info(String.format("Received request for: %s", httpExchange.getRequestURI())); if (isGetRequest(httpExchange)) { final String inMemoryResourceKey = getKeyForResource(httpExchange); if (inMemoryResources.containsKey(inMemoryResourceKey)) { final Resource resource = inMemoryResources.get(inMemoryResourceKey); sendResourceToClient(httpExchange, resource); } else { responseWith404(httpExchange, false); } } else { responseWith501(httpExchange, false); } } private void sendResourceToClient(final HttpExchange httpExchange, final Resource resoure) { final ImmutableMap<String, String> headerInfo = ImmutableMap.of(HTTP_CONTENT_TYPE, resoure.getContentType()); sendHttpOKHeaders(httpExchange, headerInfo); sendBody(httpExchange, resoure.getBytes()); } private String getKeyForResource(final HttpExchange httpExchange) { final String requestedUri = HttpUtils.getPathVariable(httpExchange); return requestedUri.replace(Statics.SERVER_INTERNAL_CONTEXT, ""); } private boolean isGetRequest(final HttpExchange httpExchange) { return httpExchange.getRequestMethod().equalsIgnoreCase(HTTP_GET); } }