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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations