use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class BlobAccessorGcsImpl method getBlob.
@Override
public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
GcsFilename gcsFileName = new GcsFilename(bucketName, fileName);
try {
GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFileName);
if (gcsFileMetadata == null)
return null;
if (gcsFileMetadata.getLength() == 0)
return null;
GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFileName, 0);
ByteBuffer byteBuffer = ByteBuffer.allocate((int) gcsFileMetadata.getLength());
gcsInputChannel.read(byteBuffer);
if (byteBuffer.position() != gcsFileMetadata.getLength()) {
logger.log(Level.SEVERE, "Byte buffer size of " + byteBuffer.position() + " is not same as content lenght of " + gcsFileMetadata.getLength());
throw new UnexpectedServerException();
}
return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata);
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex);
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class GoogleAnalyticsApi method getPageViews.
public static Map<String, Integer> getPageViews(String date) throws UnexpectedServerException {
Map<String, Integer> uriViewsMap = new HashMap<String, Integer>();
try {
while (true) {
Get apiQuery = getAnalytics().data().ga().get(// Table Id.
"ga:96325104", // Start Date YYYY-MM-DD
date, // End Date YYYY-MM-DD
date, // Metrics.
"ga:pageviews").setDimensions("ga:pagePath").setStartIndex(uriViewsMap.size() + 1).setMaxResults(10000);
GaData gaData = apiQuery.execute();
if (gaData.getRows() != null)
for (List<String> row : gaData.getRows()) uriViewsMap.put(row.get(0), Integer.parseInt(row.get(1)));
if (uriViewsMap.size() == gaData.getTotalResults())
break;
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to fetch data from Google Analytics.", e);
throw new UnexpectedServerException();
}
return uriViewsMap;
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class SEOTitleUtil method getListPageTitle.
// TYPE: CATEGORY_LIST
public static String getListPageTitle(String listName, Language language) throws UnexpectedServerException {
String listTitle = null;
try {
String fileName = "list." + language.getCode() + "." + listName;
InputStream inputStream = DataAccessor.class.getResource("curated/" + fileName).openStream();
LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
listTitle = it.nextLine().trim();
LineIterator.closeQuietly(it);
} catch (NullPointerException | IOException e) {
throw new UnexpectedServerException();
}
Map<String, String> dataModel = new HashMap<>();
if (listTitle.contains("|")) {
dataModel.put("listTitle", listTitle.substring(0, listTitle.indexOf("|")).trim());
dataModel.put("listTitleEn", listTitle.substring(listTitle.indexOf("|") + 1).trim());
} else {
dataModel.put("listTitle", listTitle);
dataModel.put("listTitleEn", "");
}
return _getPageTitle("seo_list_page", dataModel, language);
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class BlobAccessorGcsImpl method createOrUpdateBlob.
@Override
public BlobEntry createOrUpdateBlob(BlobEntry blobEntry) throws UnexpectedServerException {
GcsFilename gcsFileName = new GcsFilename(bucketName, blobEntry.getName());
Builder builder = new GcsFileOptions.Builder();
if (blobEntry.getMimeType() != null)
builder.mimeType(blobEntry.getMimeType());
if (blobEntry.getCacheControl() != null)
builder.cacheControl(blobEntry.getCacheControl());
if (blobEntry.getMetaName() != null)
builder.addUserMetadata(BlobEntry.META_NAME, blobEntry.getMetaName());
GcsFileOptions gcsFileOptions = builder.build();
try {
GcsOutputChannel gcsOutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
gcsOutputChannel.write(ByteBuffer.wrap(blobEntry.getData()));
gcsOutputChannel.close();
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to create/update blob with name '" + blobEntry.getName() + "'", ex);
throw new UnexpectedServerException();
}
return null;
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class ImageSvgUtil method getHeight.
public static int getHeight(byte[] svgData) throws UnexpectedServerException {
try {
String svg = new String(svgData, "UTF-8");
Pattern pattern = Pattern.compile(heightPattern, Pattern.DOTALL);
Matcher mHeight = pattern.matcher(svg);
mHeight.find();
String heightStr = mHeight.group(1);
return Integer.parseInt(heightStr.substring(heightStr.indexOf("\"") + 1, heightStr.lastIndexOf("\"")));
} catch (UnsupportedEncodingException e) {
throw new UnexpectedServerException();
}
}
Aggregations