Search in sources :

Example 1 with UnexpectedServerException

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();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) BlobEntryGcsImpl(com.pratilipi.data.type.gcs.BlobEntryGcsImpl) IOException(java.io.IOException) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) ByteBuffer(java.nio.ByteBuffer) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Example 2 with 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;
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) Get(com.google.api.services.analytics.Analytics.Data.Ga.Get) List(java.util.List) LinkedList(java.util.LinkedList) GaData(com.google.api.services.analytics.model.GaData) IOException(java.io.IOException)

Example 3 with UnexpectedServerException

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);
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) DataAccessor(com.pratilipi.data.DataAccessor) IOException(java.io.IOException) LineIterator(org.apache.commons.io.LineIterator)

Example 4 with UnexpectedServerException

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;
}
Also used : GcsFileOptions(com.google.appengine.tools.cloudstorage.GcsFileOptions) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Builder(com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder) IOException(java.io.IOException) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) GcsOutputChannel(com.google.appengine.tools.cloudstorage.GcsOutputChannel)

Example 5 with UnexpectedServerException

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

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