Search in sources :

Example 96 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project pratilipi by Pratilipi.

the class DocAccessorImpl method _save.

private <T> void _save(String docPath, T doc) throws UnexpectedServerException {
    try {
        byte[] blobData = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create().toJson(doc).getBytes("UTF-8");
        BlobEntry blobEntry = blobAccessor.newBlob(docPath, blobData, "application/json");
        blobAccessor.createOrUpdateBlob(blobEntry);
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) GsonBuilder(com.google.gson.GsonBuilder) BlobEntry(com.pratilipi.data.type.BlobEntry) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) GsonLongDateAdapter(com.pratilipi.common.util.GsonLongDateAdapter)

Example 97 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project pratilipi by Pratilipi.

the class RtdbAccessorFirebaseImpl method getUserPreferences.

@Override
public Map<Long, UserPreferenceRtdb> getUserPreferences(Collection<Long> userIds) throws UnexpectedServerException {
    if (userIds == null || userIds.isEmpty())
        return new HashMap<>();
    userIds = new HashSet<>(userIds);
    List<String> targetUrlList = new ArrayList<>(userIds.size());
    for (Long userId : userIds) targetUrlList.add(_getUserPreferenceDbUrl(userId));
    Map<String, BlobEntry> blobEntries = HttpUtil.doGet(targetUrlList, headersMap);
    Map<Long, UserPreferenceRtdb> userPreferences = new HashMap<>(userIds.size());
    try {
        for (Long userId : userIds) {
            BlobEntry blobEntry = blobEntries.get(_getUserPreferenceDbUrl(userId));
            String jsonStr = new String(blobEntry.getData(), "UTF-8");
            userPreferences.put(userId, _getUserPreferenceRtdb(jsonStr));
        }
    } catch (UnsupportedEncodingException e) {
        logger.log(Level.SEVERE, "Failed to parse response from Firebase.", e);
        throw new UnexpectedServerException();
    }
    return userPreferences;
}
Also used : HashMap(java.util.HashMap) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb)

Example 98 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project pratilipi by Pratilipi.

the class PratilipiDocUtil method updatePratilipiGoogleAnalyticsPageViews.

public static List<Long> updatePratilipiGoogleAnalyticsPageViews(int year, int month, int day) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    Gson gson = new Gson();
    String dateStr = year + (month < 10 ? "-0" + month : "-" + month) + (day < 10 ? "-0" + day : "-" + day);
    String fileName = "pratilipi-google-analytics/page-views/" + dateStr;
    BlobEntry blobEntry = blobAccessor.getBlob(fileName);
    if (blobEntry == null) {
        try {
            blobEntry = blobAccessor.newBlob(fileName, "{}".getBytes("UTF-8"), "application/json");
        } catch (UnsupportedEncodingException e) {
            logger.log(Level.SEVERE, e.getMessage());
            throw new UnexpectedServerException();
        }
    }
    @SuppressWarnings("serial") Map<String, Integer> oldPageViewsMap = gson.fromJson(new String(blobEntry.getData(), Charset.forName("UTF-8")), new TypeToken<Map<String, Integer>>() {
    }.getType());
    Map<String, Integer> newPageViewsMap = GoogleAnalyticsApi.getPageViews(dateStr);
    Map<String, Integer> diffPageViewsMap = new HashMap<>();
    for (Entry<String, Integer> entry : newPageViewsMap.entrySet()) if (!entry.getValue().equals(oldPageViewsMap.get(entry.getKey())))
        diffPageViewsMap.put(entry.getKey(), entry.getValue());
    Map<Long, Integer> pageViewsMap = new HashMap<>();
    Map<Long, Integer> readPageViewsMap = new HashMap<>();
    for (Entry<String, Integer> entry : diffPageViewsMap.entrySet()) {
        String uri = entry.getKey();
        if (!uri.startsWith("/read?id=")) {
            if (uri.indexOf('?') != -1)
                uri = uri.substring(0, uri.indexOf('?'));
            Page page = dataAccessor.getPage(uri);
            if (page != null && page.getType() == PageType.PRATILIPI) {
                Long pratilpiId = page.getPrimaryContentId();
                if (pageViewsMap.get(pratilpiId) == null)
                    pageViewsMap.put(pratilpiId, entry.getValue());
                else
                    pageViewsMap.put(pratilpiId, pageViewsMap.get(pratilpiId) + entry.getValue());
            }
        } else {
            // Reader
            String patilipiIdStr = uri.indexOf('&') == -1 ? uri.substring("/read?id=".length()) : uri.substring("/read?id=".length(), uri.indexOf('&'));
            try {
                Long pratilpiId = Long.parseLong(patilipiIdStr);
                if (readPageViewsMap.get(pratilpiId) == null)
                    readPageViewsMap.put(pratilpiId, entry.getValue());
                else
                    readPageViewsMap.put(pratilpiId, readPageViewsMap.get(pratilpiId) + entry.getValue());
            } catch (NumberFormatException e) {
                logger.log(Level.SEVERE, "Exception while processing reader uri " + uri, e);
            }
        }
    }
    for (Entry<Long, Integer> entry : pageViewsMap.entrySet()) {
        if (readPageViewsMap.get(entry.getKey()) == null) {
            updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), 0);
        } else {
            updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, entry.getValue(), readPageViewsMap.get(entry.getKey()));
            readPageViewsMap.remove(entry.getKey());
        }
    }
    for (Entry<Long, Integer> entry : readPageViewsMap.entrySet()) updatePratilipiGoogleAnalyticsPageViews(entry.getKey(), year, month, day, 0, entry.getValue());
    if (diffPageViewsMap.size() > 0) {
        try {
            blobEntry.setData(gson.toJson(newPageViewsMap).getBytes("UTF-8"));
            blobAccessor.createOrUpdateBlob(blobEntry);
        } catch (UnsupportedEncodingException e) {
            logger.log(Level.SEVERE, e.getMessage());
            throw new UnexpectedServerException();
        }
    }
    ArrayList<Long> updatedPratilipiIdList = new ArrayList<>(pageViewsMap.size() + readPageViewsMap.size());
    updatedPratilipiIdList.addAll(pageViewsMap.keySet());
    updatedPratilipiIdList.addAll(readPageViewsMap.keySet());
    return updatedPratilipiIdList;
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Page(com.pratilipi.data.type.Page) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) TypeToken(com.google.common.reflect.TypeToken) BlobAccessor(com.pratilipi.data.BlobAccessor)

Example 99 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class DelegationConfig method parseDocument.

private Document parseDocument(String fileName) {
    Document document = null;
    InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
    try {
        DocumentBuilder documentBuilder = XMLUtils.getSafeDocumentBuilder(false);
        document = documentBuilder.parse(is);
    } catch (UnsupportedEncodingException e) {
        AMModelBase.debug.error("DelegationConfig.parseDocument", e);
    } catch (ParserConfigurationException e) {
        AMModelBase.debug.error("DelegationConfig.parseDocument", e);
    } catch (SAXException e) {
        AMModelBase.debug.error("DelegationConfig.parseDocument", e);
    } catch (IOException e) {
        AMModelBase.debug.error("DelegationConfig.parseDocument", e);
    }
    return document;
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 100 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project camel by apache.

the class URISupport method parseQuery.

/**
     * Parses the query part of the uri (eg the parameters).
     * <p/>
     * The URI parameters will by default be URI encoded. However you can define a parameter
     * values with the syntax: <tt>key=RAW(value)</tt> which tells Camel to not encode the value,
     * and use the value as is (eg key=value) and the value has <b>not</b> been encoded.
     *
     * @param uri the uri
     * @param useRaw whether to force using raw values
     * @return the parameters, or an empty map if no parameters (eg never null)
     * @throws URISyntaxException is thrown if uri has invalid syntax.
     * @see #RAW_TOKEN_START
     * @see #RAW_TOKEN_END
     */
public static Map<String, Object> parseQuery(String uri, boolean useRaw) throws URISyntaxException {
    // must check for trailing & as the uri.split("&") will ignore those
    if (uri != null && uri.endsWith("&")) {
        throw new URISyntaxException(uri, "Invalid uri syntax: Trailing & marker found. " + "Check the uri and remove the trailing & marker.");
    }
    if (isEmpty(uri)) {
        // return an empty map
        return new LinkedHashMap<String, Object>(0);
    }
    try {
        // use a linked map so the parameters is in the same order
        Map<String, Object> rc = new LinkedHashMap<String, Object>();
        boolean isKey = true;
        boolean isValue = false;
        boolean isRaw = false;
        StringBuilder key = new StringBuilder();
        StringBuilder value = new StringBuilder();
        // parse the uri parameters char by char
        for (int i = 0; i < uri.length(); i++) {
            // current char
            char ch = uri.charAt(i);
            // look ahead of the next char
            char next;
            if (i <= uri.length() - 2) {
                next = uri.charAt(i + 1);
            } else {
                next = '';
            }
            // are we a raw value
            isRaw = value.toString().startsWith(RAW_TOKEN_START);
            // if we are in raw mode, then we keep adding until we hit the end marker
            if (isRaw) {
                if (isKey) {
                    key.append(ch);
                } else if (isValue) {
                    value.append(ch);
                }
                // we only end the raw marker if its )& or at the end of the value
                boolean end = ch == RAW_TOKEN_END.charAt(0) && (next == '&' || next == '');
                if (end) {
                    // raw value end, so add that as a parameter, and reset flags
                    addParameter(key.toString(), value.toString(), rc, useRaw || isRaw);
                    key.setLength(0);
                    value.setLength(0);
                    isKey = true;
                    isValue = false;
                    isRaw = false;
                    // skip to next as we are in raw mode and have already added the value
                    i++;
                }
                continue;
            }
            // if its a key and there is a = sign then the key ends and we are in value mode
            if (isKey && ch == '=') {
                isKey = false;
                isValue = true;
                isRaw = false;
                continue;
            }
            // the & denote parameter is ended
            if (ch == '&') {
                // parameter is ended, as we hit & separator
                String aKey = key.toString();
                // the key may be a placeholder of options which we then do not know what is
                boolean validKey = !aKey.startsWith("{{") && !aKey.endsWith("}}");
                if (validKey) {
                    addParameter(aKey, value.toString(), rc, useRaw || isRaw);
                }
                key.setLength(0);
                value.setLength(0);
                isKey = true;
                isValue = false;
                isRaw = false;
                continue;
            }
            // regular char so add it to the key or value
            if (isKey) {
                key.append(ch);
            } else if (isValue) {
                value.append(ch);
            }
        }
        // any left over parameters, then add that
        if (key.length() > 0) {
            String aKey = key.toString();
            // the key may be a placeholder of options which we then do not know what is
            boolean validKey = !aKey.startsWith("{{") && !aKey.endsWith("}}");
            if (validKey) {
                addParameter(aKey, value.toString(), rc, useRaw || isRaw);
            }
        }
        return rc;
    } catch (UnsupportedEncodingException e) {
        URISyntaxException se = new URISyntaxException(e.toString(), "Invalid encoding");
        se.initCause(e);
        throw se;
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)3108 IOException (java.io.IOException)878 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)284 InputStream (java.io.InputStream)275 ArrayList (java.util.ArrayList)268 InputStreamReader (java.io.InputStreamReader)243 File (java.io.File)234 ByteArrayInputStream (java.io.ByteArrayInputStream)209 ByteArrayOutputStream (java.io.ByteArrayOutputStream)201 FileNotFoundException (java.io.FileNotFoundException)198 HashMap (java.util.HashMap)182 MessageDigest (java.security.MessageDigest)180 BufferedReader (java.io.BufferedReader)150 URL (java.net.URL)150 Map (java.util.Map)148 OutputStreamWriter (java.io.OutputStreamWriter)145 FileOutputStream (java.io.FileOutputStream)120 MalformedURLException (java.net.MalformedURLException)110 FileInputStream (java.io.FileInputStream)107 List (java.util.List)105