Search in sources :

Example 31 with UnexpectedServerException

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();
    }
}
Also used : Pattern(java.util.regex.Pattern) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 32 with 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);
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) IOException(java.io.IOException) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 33 with UnexpectedServerException

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();
    }
}
Also used : Pattern(java.util.regex.Pattern) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 34 with 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);
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) Page(com.pratilipi.data.type.Page) URISyntaxException(java.net.URISyntaxException) File(java.io.File) Get(com.pratilipi.api.annotation.Get)

Example 35 with UnexpectedServerException

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());
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) IOException(java.io.IOException) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator) Get(com.pratilipi.api.annotation.Get)

Aggregations

UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)46 IOException (java.io.IOException)19 JsonObject (com.google.gson.JsonObject)12 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 HashMap (java.util.HashMap)12 Gson (com.google.gson.Gson)10 DataAccessor (com.pratilipi.data.DataAccessor)10 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)6 BlobEntry (com.pratilipi.data.type.BlobEntry)6 Date (java.util.Date)6 File (java.io.File)5 JsonElement (com.google.gson.JsonElement)4 Get (com.pratilipi.api.annotation.Get)4 Post (com.pratilipi.api.annotation.Post)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 OutputStream (java.io.OutputStream)4 URL (java.net.URL)4 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)3 Page (com.pratilipi.data.type.Page)3