All Downloads are FREE. Search and download functionalities are using the official Maven repository.

by.stub.yaml.stubs.StubHttpLifecycle Maven / Gradle / Ivy

/*
A Java-based HTTP stub server

Copyright (C) 2012 Alexander Zagniotov, Isa Goksu and Eric Mrak

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 .
 */

package by.stub.yaml.stubs;


import java.util.LinkedList;
import java.util.List;

/**
 * @author Alexander Zagniotov
 * @since 6/14/12, 1:21 AM
 */
public final class StubHttpLifecycle {

   private StubRequest request;
   private Object response;
   private int responseSequenceCounter = 0;

   public StubHttpLifecycle() {
      response = new StubResponse();
   }

   public void setRequest(final StubRequest request) {
      this.request = request;
   }

   public void setResponse(final Object response) {
      this.response = response;
   }

   public StubRequest getRequest() {
      return request;
   }

   public StubResponse getResponse() {
      return getActualStubbedResponse();
   }


   public List getAllResponses() {

      if (response instanceof StubResponse) {
         return new LinkedList() {{
            add((StubResponse) response);
         }};
      }

      return (LinkedList) response;
   }


   public String getRequestAuthorizationHeader() {
      return request.getHeaders().get(StubRequest.AUTH_HEADER);
   }

   public StubResponse getActualStubbedResponse() {

      if (response instanceof StubResponse) {
         return (StubResponse) response;
      }

      final List responses = (LinkedList) response;
      if (responses.size() == 0) {
         return new StubResponse();
      }

      final StubResponse sequenceStubResponse = responses.get(responseSequenceCounter);
      responseSequenceCounter = (responseSequenceCounter + 1 == responses.size() ? responseSequenceCounter = 0 : ++responseSequenceCounter);

      return sequenceStubResponse;
   }

   @Override
   public boolean equals(final Object o) {
      if (this == o) return true;
      if (!(o instanceof StubHttpLifecycle)) return false;

      final StubHttpLifecycle that = (StubHttpLifecycle) o;

      if (!request.equals(that.request)) return false;

      return true;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy