com.cognifide.aet.rest.MetadataServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.cognifide.aet.rest.MetadataServlet.java

Source

/**
 * Automated Exploratory Tests
 *
 * Copyright (C) 2013 Cognifide Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cognifide.aet.rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import com.cognifide.aet.communication.api.exceptions.AETException;
import com.cognifide.aet.communication.api.metadata.Suite;
import com.cognifide.aet.communication.api.metadata.ValidatorException;
import com.cognifide.aet.vs.DBKey;
import com.cognifide.aet.vs.MetadataDAO;
import com.cognifide.aet.vs.StorageException;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
@Component(label = "MetadataServlet", description = "Returns Suite Metadata", immediate = true, metatype = true, policy = ConfigurationPolicy.IGNORE)
public class MetadataServlet extends BasicDataServlet {

    private static final long serialVersionUID = 100244101178249562L;

    private static final Logger LOGGER = LoggerFactory.getLogger(MetadataServlet.class);

    private static final String FORMATTED_PARAM = "formatted";

    @Reference
    private MetadataDAO metadataDAO;

    @Reference
    private LockService lockService;

    @Override
    protected void process(DBKey dbKey, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String correlationId = req.getParameter(Helper.CORRELATION_ID_PARAM);
        String suiteName = req.getParameter(Helper.SUITE_PARAM);
        String formatted = req.getParameter(FORMATTED_PARAM);
        resp.setCharacterEncoding("UTF-8");

        Suite suite;

        try {
            if (isValidCorrelationId(correlationId)) {
                suite = metadataDAO.getSuite(dbKey, correlationId);
            } else if (isValidName(suiteName)) {
                suite = metadataDAO.getLatestRun(dbKey, suiteName);
            } else {
                resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
                resp.getWriter().write(responseAsJson("Neither valid correlationId or suite param was specified."));
                return;
            }
        } catch (StorageException e) {
            LOGGER.error("Failed to get suite", e);
            resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
            resp.getWriter().write(responseAsJson("Failed to get suite: %s", e.getMessage()));
            return;
        }
        GsonBuilder gsonBuilder = new GsonBuilder();
        if (formatted != null && "true".equals(formatted)) {
            gsonBuilder.setPrettyPrinting();
        }
        String result = gsonBuilder.create().toJson(suite, Suite.class);

        resp.setContentType("application/json");
        if (result != null) {
            resp.getWriter().write(result);
        } else {
            resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
            resp.getWriter().write(responseAsJson("Unable to get Suite Metadata for %s", dbKey.toString()));
        }
    }

    /***
     * Saves Suite parsed from json provided in post body.
     * It also increments Version of Suite.
     *
     * @param req  Request
     * @param resp Response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("POST: " + req.toString());
        }
        resp.setContentType("application/json");
        try {
            updateSuite(req);
            resp.setStatus(HttpURLConnection.HTTP_OK);
        } catch (AETException e) {
            LOGGER.debug("Suite is locked.", e);
            resp.setStatus(HttpURLConnection.HTTP_CONFLICT);
            resp.getWriter().write(responseAsJson(e.getMessage()));
        } catch (JsonSyntaxException | JsonIOException jsonSyntaxException) {
            LOGGER.error("Invalid json provided by client", jsonSyntaxException);
            resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
            resp.getWriter().write(
                    responseAsJson("Error: Invalid format of provided Json %s", jsonSyntaxException.getMessage()));
        } catch (ValidatorException validatorException) {
            LOGGER.error("Invalid json provided by client: {}", validatorException.getIssues(), validatorException);
            resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
            resp.getWriter()
                    .write(responseAsJson("Invalid Suite representation : %s", validatorException.getIssues()));
        } catch (StorageException e) {
            LOGGER.error("Failed to save suite", e);
            resp.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
            resp.getWriter().write(responseAsJson("ERROR: Unable to save provided Suite. %s", e.getMessage()));
        } finally {
            resp.flushBuffer();
        }
    }

    private void updateSuite(HttpServletRequest req)
            throws StorageException, ValidatorException, AETException, IOException {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8));
        Suite suite = new Gson().fromJson(reader, Suite.class);
        checkLock(suite.getSuiteIdentifier());
        metadataDAO.updateSuite(suite);
    }

    private void checkLock(String suiteIdentifier) throws AETException {
        if (lockService.isLockPresent(suiteIdentifier)) {
            throw new AETException("Suite modification is currently locked. please try again later.");
        }
    }

    @Activate
    public void start() {
        register(Helper.getMetadataPath());

    }

    @Deactivate
    public void stop() {
        unregister(Helper.getMetadataPath());
    }
}