Search in sources :

Example 11 with HttpResponse

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

the class HttpDiskNodeClient method deleteDir.

@Override
public boolean deleteDir(String path, boolean force, boolean recursive) {
    URIBuilder builder = new URIBuilder();
    builder.setScheme(DEFAULT_SCHEME).setHost(host).setPort(port).setPath(DiskContext.URI_DISK_NODE_ROOT + path);
    if (force) {
        builder.addParameter("force");
    }
    if (recursive) {
        builder.addParameter("recursive");
    }
    try {
        LOG.info("delete dir[{}] force[{}] recursive[{}] to {}:{}", path, force, recursive, host, port);
        HttpResponse response = client.executeDelete(builder.build());
        LOG.debug("delete dir[{}] response[{}]", path, response.getStatusCode());
        return response.isReponseOK();
    } catch (Exception e) {
        LOG.error("delete dir[{}] at {}:{} error", path, host, port, e);
    }
    return false;
}
Also used : HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 12 with HttpResponse

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

the class HttpDiskNodeClient method writeDatas.

@Override
public WriteResult[] writeDatas(String path, List<byte[]> dataList) throws IOException {
    WriteDataList datas = new WriteDataList();
    WriteData[] dataArray = new WriteData[dataList.size()];
    for (int i = 0; i < dataArray.length; i++) {
        WriteData data = new WriteData();
        data.setBytes(dataList.get(i));
        dataArray[i] = data;
    }
    datas.setDatas(dataArray);
    URI uri = new URIBuilder().setScheme(DEFAULT_SCHEME).setHost(host).setPort(port).setPath(DiskContext.URI_DISK_NODE_ROOT + path).build();
    try {
        LOG.info("write file[{}] with {} datas to {}:{}", path, dataList.size(), host, port);
        HttpResponse response = client.executePost(uri, ProtoStuffUtils.serialize(datas));
        LOG.debug("write file[{}] response[{}]", path, response.getStatusCode());
        if (response.isReponseOK()) {
            WriteResultList resultList = ProtoStuffUtils.deserialize(response.getResponseBody(), WriteResultList.class);
            return resultList.getWriteResults();
        }
    } catch (Exception e) {
        LOG.error("write file[{}] to {}:{} error", path, host, port, e);
    }
    return null;
}
Also used : WriteDataList(com.bonree.brfs.disknode.server.handler.data.WriteDataList) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) WriteData(com.bonree.brfs.disknode.server.handler.data.WriteData) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder)

Example 13 with HttpResponse

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

the class DefaultBRFileSystem method createStorageName.

@Override
public boolean createStorageName(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 {
                response = httpClient.executePut(uriBuilder.build(), defaultHeaders);
            } catch (Exception e) {
                LOG.warn("createStorageName http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for createStorageName!");
            }
            if (response.isReponseOK()) {
                return true;
            }
        }
    } catch (Exception e) {
        LOG.error("createStorageName error", e);
    }
    return false;
}
Also used : 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 14 with HttpResponse

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

the class DefaultBRFileSystem method openStorageName.

@Override
public StorageNameStick openStorageName(String storageName) {
    StorageNameStick stick = null;
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getStorageUrlRoot() + "/" + storageName).build();
            HttpResponse response = null;
            try {
                response = httpClient.executeGet(uri, defaultHeaders);
            } catch (Exception e) {
                LOG.warn("openStorageName http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for deleteStorageName!");
            }
            if (response.isReponseOK()) {
                int storageId = Ints.fromByteArray(response.getResponseBody());
                stick = new DefaultStorageNameStick(storageName, storageId, httpClient, serviceSelectorManager.useDiskSelector(storageId), regionNodeSelector, config);
                return stick;
            }
        }
    } catch (Exception e) {
        LOG.error("openStorageName error", e);
    }
    return stick;
}
Also used : StorageNameStick(com.bonree.brfs.client.StorageNameStick) 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 15 with HttpResponse

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

the class DefaultStorageNameStick method writeData.

@Override
public String[] writeData(InputItem[] itemArrays) {
    WriteDataMessage dataMessage = new WriteDataMessage();
    dataMessage.setStorageNameId(storageId);
    DataItem[] dataItems = new DataItem[itemArrays.length];
    for (int i = 0; i < dataItems.length; i++) {
        dataItems[i] = new DataItem();
        dataItems[i].setBytes(itemArrays[i].getBytes());
    }
    dataMessage.setItems(dataItems);
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getDuplicateUrlRoot() + "/").build();
            HttpResponse response = null;
            try {
                response = client.executePost(uri, defaultHeaders, ProtoStuffUtils.serialize(dataMessage));
            } catch (Exception e) {
                LOG.warn("write data http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for writeData!");
            }
            if (response.isReponseOK()) {
                List<String> results = JSON.parseArray(BrStringUtils.fromUtf8Bytes(response.getResponseBody()), String.class);
                String[] fids = new String[results.size()];
                return results.toArray(fids);
            }
        }
    } catch (Exception e) {
        LOG.error("write data error", e);
    }
    return null;
}
Also used : WriteDataMessage(com.bonree.brfs.common.write.data.WriteDataMessage) DataItem(com.bonree.brfs.common.write.data.DataItem) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder) BRFSException(com.bonree.brfs.common.exception.BRFSException)

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