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

org.openstack4j.openstack.compute.functions.ToActionResponseFunction Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package org.openstack4j.openstack.compute.functions;

import org.openstack4j.core.transport.HttpEntityHandler;
import org.openstack4j.core.transport.HttpResponse;
import org.openstack4j.core.transport.functions.ResponseToActionResponse;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.openstack.logging.Logger;
import org.openstack4j.openstack.logging.LoggerFactory;

import com.google.common.base.Function;

/**
 * A Function which consumes the input of an HttpResponse and returns the
 * corresponding ActionResponse
 *
 * @author Jeremy Unruh
 */
public class ToActionResponseFunction implements Function {

    public static final ToActionResponseFunction INSTANCE = new ToActionResponseFunction();
    private static final Logger LOG = LoggerFactory.getLogger(ToActionResponseFunction.class);
    private static final String COMMA = ",";
    private static final String FAILED_MSG = "Cannot '%s' while instance in in state of %s";

    @Override
    public ActionResponse apply(HttpResponse response) {
        return apply(response, null);
    }

    public ActionResponse apply(HttpResponse response, String action) {
        try {
            if (response.getStatus() == 409 || response.getStatus() == 500) {
                ActionResponse ar = ResponseToActionResponse.INSTANCE.apply(response, true);
                if (ar != null) {
                    return ar;
                }

                LOG.error(response.getStatus() + COMMA + response.getStatusMessage());
                if (action == null) {
                    return ActionResponse.actionFailed("Instance currently is in build state", 409);
                }
                return ActionResponse.actionFailed(String.format(FAILED_MSG, action, action), 409);
            }
            if (response.getStatus() >= 400 && response.getStatus() < 409) {
                return ResponseToActionResponse.INSTANCE.apply(response);
            }
            return ActionResponse.actionSuccess();
        } finally {
            HttpEntityHandler.closeQuietly(response);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy