Java tutorial
/* * RESTHeart - the data REST API server * Copyright (C) 2014 - 2015 SoftInstigate Srl * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.restheart.handlers.database; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import org.restheart.handlers.injectors.LocalCachesSingleton; import org.restheart.handlers.PipedHttpHandler; import org.restheart.utils.HttpStatus; import org.restheart.handlers.RequestContext; import org.restheart.utils.RequestHelper; import org.restheart.utils.ResponseHelper; import io.undertow.server.HttpServerExchange; import org.bson.types.ObjectId; import org.restheart.hal.metadata.InvalidMetadataException; import org.restheart.hal.metadata.RepresentationTransformer; /** * * @author Andrea Di Cesare <andrea@softinstigate.com> */ public class PutDBHandler extends PipedHttpHandler { /** * Creates a new instance of PutDBHandler */ public PutDBHandler() { super(); } /** * * @param exchange * @param context * @throws Exception */ @Override public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception { if (context.getDBName().isEmpty() || context.getDBName().startsWith("_")) { ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "db name cannot be empty or start with _"); return; } DBObject content = context.getContent(); if (content == null) { content = new BasicDBObject(); } // cannot PUT an array if (content instanceof BasicDBList) { ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data cannot be an array"); return; } // check RTL metadata if (content.containsField(RepresentationTransformer.RTS_ELEMENT_NAME)) { try { RepresentationTransformer.getFromJson(content); } catch (InvalidMetadataException ex) { ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "wrong representation transform logic definition. " + ex.getMessage(), ex); return; } } ObjectId etag = RequestHelper.getWriteEtag(exchange); int httpCode = getDatabase().upsertDB(context.getDBName(), content, etag, false); // send the warnings if any (and in case no_content change the return code to ok if (context.getWarnings() != null && !context.getWarnings().isEmpty()) { sendWarnings(httpCode, exchange, context); } else { exchange.setResponseCode(httpCode); } if (httpCode == HttpStatus.SC_CREATED || httpCode == HttpStatus.SC_OK) { content.put("_etag", etag); ResponseHelper.injectEtagHeader(exchange, content); } exchange.endExchange(); LocalCachesSingleton.getInstance().invalidateDb(context.getDBName()); } }