Search in sources :

Example 6 with HttpResponse

use of com.bonree.brfs.common.net.http.client.HttpResponse in project BRFS by zhangnianli.

the class DefaultBRFileSystem method updateStorageName.

@Override
public boolean updateStorageName(String storageName, Map<String, Object> attrs) {
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URIBuilder uriBuilder = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName);
            for (Entry<String, Object> attr : attrs.entrySet()) {
                uriBuilder.addParameter(attr.getKey(), String.valueOf(attr.getValue()));
            }
            HttpResponse response = null;
            try {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("username", config.getName());
                headers.put("password", config.getPasswd());
                response = httpClient.executePost(uriBuilder.build(), headers);
            } catch (Exception e) {
                LOG.warn("updateStorageName http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for updateStorageName!");
            }
            if (response.isReponseOK()) {
                return true;
            }
        }
    } catch (Exception e) {
        LOG.error("updateStorageName error", e);
    }
    return false;
}
Also used : HashMap(java.util.HashMap) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) BRFSException(com.bonree.brfs.common.exception.BRFSException) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 7 with HttpResponse

use of com.bonree.brfs.common.net.http.client.HttpResponse in project BRFS by zhangnianli.

the class DefaultStorageNameStick method deleteData.

@Override
public boolean deleteData(DateTime startTime, DateTime endTime) {
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            StringBuilder pathBuilder = new StringBuilder();
            pathBuilder.append(config.getDuplicateUrlRoot()).append("/").append(storageId).append("/").append(startTime.toString()).append("_").append(endTime.toString());
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(pathBuilder.toString()).build();
            HttpResponse response = null;
            try {
                response = client.executeDelete(uri, defaultHeaders);
            } catch (Exception e) {
                LOG.warn("delete data http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for createStorageName!");
            }
            if (response.isReponseOK()) {
                return true;
            }
            String code = new String(response.getResponseBody(), StandardCharsets.UTF_8);
            ReturnCode returnCode = ReturnCode.checkCode(storageName, code);
            LOG.info("returnCode:" + returnCode);
        }
    } catch (Exception e) {
        LOG.error("delete data error", e);
    }
    return false;
}
Also used : ReturnCode(com.bonree.brfs.common.ReturnCode) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) BRFSException(com.bonree.brfs.common.exception.BRFSException) URI(java.net.URI) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 8 with HttpResponse

use of com.bonree.brfs.common.net.http.client.HttpResponse in project BRFS by zhangnianli.

the class HttpDiskNodeClient method getFileLength.

@Override
public long getFileLength(String path) {
    URI uri = new URIBuilder().setScheme(DEFAULT_SCHEME).setHost(host).setPort(port).setPath(DiskContext.URI_LENGTH_NODE_ROOT + path).build();
    try {
        LOG.info("get length from file[{}] to {}:{}", path, host, port);
        HttpResponse response = client.executeGet(uri);
        LOG.debug("get length from file[{}] response[{}]", path, response.getStatusCode());
        if (response.isReponseOK()) {
            return Longs.fromByteArray(response.getResponseBody());
        }
    } catch (Exception e) {
        LOG.error("get sequences of file[{}] at {}:{} error", path, host, port, e);
    }
    return -1;
}
Also used : HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 9 with HttpResponse

use of com.bonree.brfs.common.net.http.client.HttpResponse in project BRFS by zhangnianli.

the class HttpDiskNodeClient method readData.

@Override
public void readData(String path, long offset, int size, ByteConsumer consumer) throws IOException {
    URI uri = new URIBuilder().setScheme(DEFAULT_SCHEME).setHost(host).setPort(port).setPath(DiskContext.URI_DISK_NODE_ROOT + path).addParameter("offset", String.valueOf(offset)).addParameter("size", String.valueOf(size)).build();
    try {
        LOG.info("read file[{}] with offset[{}], size[{}] to {}:{}", path, offset, size, host, port);
        HttpResponse response = client.executeGet(uri);
        LOG.debug("read file[{}] response[{}]", path, response.getStatusCode());
        if (response.isReponseOK()) {
            consumer.consume(response.getResponseBody(), true);
        }
    } catch (Exception e) {
        LOG.error("read file[{}] with[offset={},size={}] at {}:{} error", path, offset, size, host, port, e);
    }
}
Also used : HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 10 with HttpResponse

use of com.bonree.brfs.common.net.http.client.HttpResponse in project BRFS by zhangnianli.

the class HttpDiskNodeClient method closeFile.

@Override
public long closeFile(String path) {
    URI uri = new URIBuilder().setScheme(DEFAULT_SCHEME).setHost(host).setPort(port).setPath(DiskContext.URI_DISK_NODE_ROOT + path).build();
    try {
        LOG.info("close file[{}] to {}:{}", path, host, port);
        HttpResponse response = client.executeClose(uri);
        LOG.debug("close file[{}] response[{}]", path, response.getStatusCode());
        if (response.isReponseOK()) {
            return Longs.fromByteArray(response.getResponseBody());
        }
    } catch (Exception e) {
        LOG.error("close file[{}] at {}:{} error", path, host, port, e);
    }
    return -1;
}
Also used : HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Aggregations

HttpResponse (com.bonree.brfs.common.net.http.client.HttpResponse)15 URIBuilder (com.bonree.brfs.common.net.http.client.URIBuilder)15 IOException (java.io.IOException)15 URI (java.net.URI)11 BRFSException (com.bonree.brfs.common.exception.BRFSException)6 Service (com.bonree.brfs.common.service.Service)6 StorageNameStick (com.bonree.brfs.client.StorageNameStick)1 ReturnCode (com.bonree.brfs.common.ReturnCode)1 DataItem (com.bonree.brfs.common.write.data.DataItem)1 WriteDataMessage (com.bonree.brfs.common.write.data.WriteDataMessage)1 WriteData (com.bonree.brfs.disknode.server.handler.data.WriteData)1 WriteDataList (com.bonree.brfs.disknode.server.handler.data.WriteDataList)1 HashMap (java.util.HashMap)1