use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class ImageSvgUtil method resizeSvg.
public static byte[] resizeSvg(byte[] svgData, int width, int height) throws UnexpectedServerException {
try {
String svgStr = new String(svgData, "UTF-8");
Pattern pWidth = Pattern.compile(widthPattern, Pattern.DOTALL);
Matcher mWidth = pWidth.matcher(svgStr);
mWidth.find();
String widthStr = mWidth.group(1).trim();
Pattern pHeight = Pattern.compile(heightPattern, Pattern.DOTALL);
Matcher mHeight = pHeight.matcher(svgStr);
mHeight.find();
String heightStr = mHeight.group(1).trim();
if (height == 0)
height = width * Integer.parseInt(heightStr.substring(heightStr.indexOf("\"") + 1, heightStr.lastIndexOf("\""))) / Integer.parseInt(widthStr.substring(widthStr.indexOf("\"") + 1, widthStr.lastIndexOf("\"")));
return svgStr.replace(widthStr, "width=\"" + width + "\"").replace(heightStr, "height=\"" + height + "\"").getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class HttpUtil method _toBlobEntry.
private static BlobEntry _toBlobEntry(String targetUrl, HTTPResponse response) throws UnexpectedServerException {
String mimeType = null;
int status = response.getResponseCode();
byte[] data = response.getContent();
for (HTTPHeader header : response.getHeadersUncombined()) {
if (header.getName().equals("Content-Type")) {
mimeType = header.getValue();
break;
}
}
logger.log(Level.INFO, "Http GET Request: " + targetUrl);
logger.log(Level.INFO, "Status: " + status);
logger.log(Level.INFO, "Type: " + mimeType);
logger.log(Level.INFO, "Length: " + data.length);
if (status != 200) {
try {
logger.log(Level.SEVERE, "Response: " + new String(data, "UTF-8"));
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to parse error response.", e);
}
throw new UnexpectedServerException();
}
return DataAccessorFactory.getBlobAccessor().newBlob(null, data, mimeType);
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class ImageSvgUtil method getWidth.
public static int getWidth(byte[] svgData) throws UnexpectedServerException {
try {
String svgStr = new String(svgData, "UTF-8");
Pattern pattern = Pattern.compile(widthPattern, Pattern.DOTALL);
Matcher mWidth = pattern.matcher(svgStr);
mWidth.find();
String widthStr = mWidth.group(1);
return Integer.parseInt(widthStr.substring(widthStr.indexOf("\"") + 1, widthStr.lastIndexOf("\"")));
} catch (UnsupportedEncodingException e) {
throw new UnexpectedServerException();
}
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class PageApi method get.
@Get
public Response get(GetRequest request) throws InvalidArgumentException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
if (request.uri.startsWith("/read?id=")) {
// TODO: Remove this as soon as READ uri(s) are added to Page table.
String pratilipiId = request.uri.substring("/read?id=".length());
if (pratilipiId.indexOf('&') != -1)
pratilipiId = pratilipiId.substring(0, pratilipiId.indexOf('&'));
return new Response(PageType.READ, Long.parseLong(pratilipiId));
}
Page page = dataAccessor.getPage(request.uri);
if (page == null && request.uri.contains("?"))
page = dataAccessor.getPage(request.uri.substring(0, request.uri.indexOf("?")));
if (page != null)
return new Response(page.getType(), page.getPrimaryContentId());
if (request.uri.matches("^/[a-z0-9-]+$")) {
// TODO: Remove this as soon as CATEGORY_LIST uri(s) are added to Page table.
try {
String folder = DataAccessor.class.getResource("curated/").toURI().getPath();
for (String fileName : new File(folder).list()) if (fileName.matches("list[.]\\w\\w[.]" + request.uri.substring(1)))
return new Response(PageType.CATEGORY_LIST, fileName.substring(fileName.lastIndexOf('.') + 1));
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Failed to list category list files.", e);
throw new UnexpectedServerException();
}
}
JsonObject errorMessages = new JsonObject();
errorMessages.addProperty("uri", "Invalid uri !");
throw new InvalidArgumentException(errorMessages);
}
use of com.pratilipi.common.exception.UnexpectedServerException in project pratilipi by Pratilipi.
the class PageContentApi method get.
@Get
public Response get(GetRequest request) throws UnexpectedServerException {
String title = null;
StringBuilder content = new StringBuilder();
try {
File file = _getFile(_getFileName(request.pageName, request.language));
if (// Fall-back to English
file == null && request.language != Language.ENGLISH)
file = _getFile(_getFileName(request.pageName, Language.ENGLISH));
if (// File doesn't exist in specified language and English
file == null)
return new Response();
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
if (it.hasNext())
title = it.nextLine().trim();
while (it.hasNext()) content.append(it.nextLine() + "<br/>");
LineIterator.closeQuietly(it);
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception while reading from data file.", e);
throw new UnexpectedServerException();
}
return new Response(title, content.toString());
}
Aggregations