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

org.bdware.doip.httprepo.HttpDO Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.httprepo;

import com.google.gson.JsonObject;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.operations.BasicOperations;

import java.nio.charset.StandardCharsets;

public class HttpDO extends DigitalObject {
    static DoipMessageFactory factory = new DoipMessageFactory();

    public HttpDO(String id, DoType type) {
        super(id, type);
    }

    public static HttpDO asHttpDO(DigitalObject digitalObject) {
        HttpDO httpDO = new HttpDO(digitalObject.id, digitalObject.type);
        httpDO.attributes = digitalObject.attributes;
        httpDO.elements = digitalObject.elements;
        return httpDO;
    }

    public int getResponseCode() {
        return attributes.get("responseCode").getAsInt();
    }

    public int getResponseMessage() {
        return attributes.get("responseMessage").getAsInt();
    }

    public JsonObject getHeader() {
        return attributes.get("headers").getAsJsonObject();
    }

    public String getBodyAsString() {
        return new String(elements.get(0).getData(), StandardCharsets.UTF_8);
    }

    public byte[] getBodyAsBytes() {
        return (elements.get(0).getData());
    }

    public String getErrorAsString() {
        try {
            return new String(elements.get(1).getData(), StandardCharsets.UTF_8);
        } catch (Throwable t) {
        }
        return null;
    }

    public byte[] getErrorAsBytes() {
        try {
            return (elements.get(1).getData());
        } catch (Throwable t) {
        }
        return null;
    }

    public static HttpDO createDO(String url, String method, JsonObject headers) {
        HttpDO digitalObject = new HttpDO(null, DoType.DO);
        digitalObject.addAttribute("url", url);
        digitalObject.addAttribute("method", method);
        digitalObject.attributes.add("headers", headers);
        return digitalObject;
    }

    public static HttpDO updateDO(String doid, String url, String method, JsonObject headers) {
        HttpDO digitalObject = new HttpDO(doid, DoType.DO);
        digitalObject.addAttribute("url", url);
        digitalObject.addAttribute("method", method);
        digitalObject.attributes.add("headers", headers);
        return digitalObject;
    }

    public static DoipMessage retrieveMsg(String doid, String query, JsonObject headers, byte[] body) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        builder.createRequest(doid, BasicOperations.Retrieve.getName());
        builder.addAttributes("query", query);
        builder.addAttributes("headers", headers);
        builder.setBody(body);
        return builder.create();
    }

    public static DoipMessage helloMsg(String doid) {
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        builder.createRequest(doid, BasicOperations.Hello.getName());
        return builder.create();
    }
    //delete is not supported
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy