Search in sources :

Example 6 with InternalServerErrorException

use of net.runelite.http.service.util.exception.InternalServerErrorException in project runelite by runelite.

the class XteaService method checkKeys.

private boolean checkKeys(CacheEntry cache, int regionId, int[] keys) {
    int x = regionId >>> 8;
    int y = regionId & 0xFF;
    String archiveName = new StringBuilder().append('l').append(x).append('_').append(y).toString();
    int archiveNameHash = Djb2.hash(archiveName);
    ArchiveEntry archiveEntry = cacheService.findArchiveForTypeAndName(cache, IndexType.MAPS, archiveNameHash);
    if (archiveEntry == null) {
        throw new InternalServerErrorException("Unable to find archive for region");
    }
    byte[] data = cacheService.getArchive(archiveEntry);
    if (data == null) {
        throw new InternalServerErrorException("Unable to get archive data");
    }
    try {
        Container.decompress(data, keys);
        return true;
    } catch (IOException ex) {
        return false;
    }
}
Also used : InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ArchiveEntry(net.runelite.http.service.cache.beans.ArchiveEntry) IOException(java.io.IOException)

Example 7 with InternalServerErrorException

use of net.runelite.http.service.util.exception.InternalServerErrorException in project runelite by runelite.

the class BlogService method getBlogPosts.

public List<FeedItem> getBlogPosts() throws IOException {
    Request request = new Request.Builder().url(RSS_URL).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        if (!response.isSuccessful()) {
            throw new IOException("Error getting blog posts: " + response.message());
        }
        try {
            InputStream in = response.body().byteStream();
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
            Element documentElement = document.getDocumentElement();
            NodeList documentItems = documentElement.getElementsByTagName("entry");
            List<FeedItem> items = new ArrayList<>();
            for (int i = 0; i < Math.min(documentItems.getLength(), 3); i++) {
                Node item = documentItems.item(i);
                NodeList children = item.getChildNodes();
                String title = null;
                String summary = null;
                String link = null;
                long timestamp = -1;
                for (int j = 0; j < children.getLength(); j++) {
                    Node childItem = children.item(j);
                    String nodeName = childItem.getNodeName();
                    switch(nodeName) {
                        case "title":
                            title = childItem.getTextContent();
                            break;
                        case "summary":
                            summary = childItem.getTextContent().replace("\n", "").trim();
                            break;
                        case "link":
                            link = childItem.getAttributes().getNamedItem("href").getTextContent();
                            break;
                        case "updated":
                            timestamp = DATE_FORMAT.parse(childItem.getTextContent()).getTime();
                            break;
                    }
                }
                if (title == null || summary == null || link == null || timestamp == -1) {
                    throw new InternalServerErrorException("Failed to find title, summary, link and/or timestamp in the blog post feed");
                }
                items.add(new FeedItem(FeedItemType.BLOG_POST, title, summary, link, timestamp));
            }
            return items;
        } catch (ParserConfigurationException | SAXException | ParseException e) {
            throw new InternalServerErrorException("Failed to parse blog posts: " + e.getMessage());
        }
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Request(okhttp3.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) Response(okhttp3.Response) FeedItem(net.runelite.http.api.feed.FeedItem) InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParseException(java.text.ParseException)

Aggregations

InternalServerErrorException (net.runelite.http.service.util.exception.InternalServerErrorException)7 IOException (java.io.IOException)5 Request (okhttp3.Request)5 Response (okhttp3.Response)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)3 FeedItem (net.runelite.http.api.feed.FeedItem)3 InputStreamReader (java.io.InputStreamReader)2 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 HttpUrl (okhttp3.HttpUrl)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 SAXException (org.xml.sax.SAXException)2 Type (java.lang.reflect.Type)1 List (java.util.List)1 FeedItemType (net.runelite.http.api.feed.FeedItemType)1 HiscoreEndpoint (net.runelite.http.api.hiscore.HiscoreEndpoint)1