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();
}
}
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;
}
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;
}
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;
}
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 = '