Package net.plan99.payfile

Examples of net.plan99.payfile.Payfile$File$Builder


        List<AtomicItem> list = new ArrayList<AtomicItem>(divs.length);
        for (int index = 0; index < divs.length; index++) {
            Div d = divs[index];
            if ("page".equals(d.getTYPE())) {
                Fptr fptr = d.getFptr(0);
                File file = (File) fptr.getFILEID();
                String groupId = file.getGROUPID();
                int size = list.size();
                if (size == 0 || (size > 0 && !(groupId.equals(list.get(size - 1).getId())))) {
                    AtomicItem item = parsePage(d);
                    item.setId(groupId);
                    list.add(item);
View Full Code Here


                    if (dataStream != null) {
                        Map<String, Map<String, String>> valueMap = dataStream.getValueMap();
                        for (Map.Entry<String, Map<String, String>> entry : valueMap.entrySet()) {
                            Map<String, String> stream = new HashMap<String, String>();
                            for (Fptr fptr2: fptrs) {
                                File file = (File)fptr2.getFILEID();
                                String use = file.getUSE();
                                if (entry.getValue().get(use) != null) {
                                    stream.put(use, file.getFLocat(0).getHref());
                                }
                            }
                            metsObj.getDataStreamsMap().put(entry.getKey(), stream);
                        }
                    }                   
                }
            }

            // Now we can process logicalStruct
            for (int i = 0; i < maps.length; i++) {
                StructMap structMap = maps[i];
                Div div = structMap.getDiv();
                String type = structMap.getTYPE();
                if (MetsConstants.LOGICAL.equals(type)) {
                    Map<String, CompositeItem> cimap = new HashMap<String, CompositeItem>();               
                    metsObj.setCompositeItemMap(cimap);
                    metsObj.setDescription(div.getLABEL());
                   
                    Fptr[] fptrs = div.getFptr();
                    ParameterMap dataStream = config.getDataStream();
                    if (dataStream != null) {
                        Map<String, Map<String, String>> valueMap = dataStream.getValueMap();
                        for (Map.Entry<String, Map<String, String>> entry : valueMap.entrySet()) {
                            Map<String, String> stream = new HashMap<String, String>();
                            for (Fptr fptr2: fptrs) {
                                File file = (File)fptr2.getFILEID();
                                String use = file.getUSE();
                                if (entry.getValue().get(use) != null) {
                                    stream.put(use, file.getFLocat(0).getHref());
                                }
                            }
                            metsObj.getDataStreamsMap().put(entry.getKey(), stream);
                        }
                    }
View Full Code Here

    protected Object parseLogicalStruct(String oid, Div div, CompositeItem parent)
            throws DAOException {
        if ("page".equals(div.getTYPE()) || div.getDivCount() == 0) {
            if (div.getFptrCount() > 0) {
                Fptr fptr = div.getFptr(0);
                File file = (File) fptr.getFILEID();
                String groupId = file.getGROUPID();
                AtomicItem item = metsObj.getAtomicItemByID(groupId);
                item.setParent(parent);
                return item;
            } else if (div.getMptrCount() > 0) {
                Mptr mptr = div.getMptr(0);
                MetsPointerAtomicItem item = new MetsPointerAtomicItem();
                item.setType(div.getTYPE());
                item.setLabel(div.getLABEL());
                item.setParent(parent);
                item.setHref(mptr.getHref());
                return item;
            } else {
                return null;
            }
        } else {
            String type = div.getTYPE();
            String label = div.getLABEL();
            CompositeItem division = new CompositeItem();
            division.setType(type);
            division.setLabel(label);
            if (div.getCONTENTIDSCount() > 0) {
              division.setContentId(div.getCONTENTIDS(0));
            }
            if (StringUtils.isBlank(div.getID())) {
                division.setId(generateUniqueID());
            } else {
                division.setId(div.getID());
            }
            metsObj.getCompositeItemMap().put(division.getId(), division);
            if (div.getDMDIDCount() == 1) {
                DmdSec dmd = (DmdSec) div.getDMDID(0);
                division.setDmdID(dmd.getID());
            }

            Div[] divs = div.getDiv();
            division.setChildren(new ArrayList<AbstractItem>(divs.length));
            for (int i = 0; i < divs.length; i++) {
                Div d = divs[i];
                Object obj = parseLogicalStruct(oid, d, division);
                if (obj instanceof AtomicItem) {
                    // Add page to division's child list
                    division.addChild((AtomicItem) obj);
                    if (i == 0) {
                        division.setFirst((AtomicItem) obj);
                    }
                    if (i == divs.length - 1) {
                        division.setLast((AtomicItem) obj);
                    }
                } else if (obj instanceof CompositeItem) {
                    division.addChild((CompositeItem) obj);
                    if (i == 0) {
                        division.setFirst(((CompositeItem) obj).getFirst());
                    }
                    if (i == divs.length - 1) {
                        division.setLast(((CompositeItem) obj).getLast());
                    }
                }
            }

            Fptr[] fptrs = div.getFptr();
            ParameterMap dataStream = config.getDataStream();
            if (dataStream != null) {
                Map<String, Map<String, String>> valueMap = dataStream.getValueMap();
                for (Map.Entry<String, Map<String, String>> entry : valueMap.entrySet()) {
                    Map<String, String> stream = new HashMap<String, String>();
                    for (Fptr fptr2: fptrs) {
                        File file = (File)fptr2.getFILEID();
                        String use = file.getUSE();
                        if (entry.getValue().get(use) != null) {
                            stream.put(use, file.getFLocat(0).getHref());
                        }
                    }
                    division.getDataStreamsMap().put(entry.getKey(), stream);
                }
            }
View Full Code Here

                sendError(e);
            } catch (IOException ignored) {}
        } catch (Throwable t) {
            // Internal server error.
            try {
                sendError(new ProtocolException(ProtocolException.Code.INTERNAL_ERROR, "Internal server error: " + t.toString()));
            } catch (IOException ignored) {}
        } finally {
            forceClose();
        }
    }
View Full Code Here

                break;
            case DOWNLOAD_CHUNK:
                downloadChunk(msg.getDownloadChunk());
                break;
            default:
                throw new ProtocolException("Unknown message");
        }
    }
View Full Code Here

    private void checkForNetworkMismatch(Payfile.QueryFiles queryFiles) throws ProtocolException {
        final String theirNetwork = queryFiles.getBitcoinNetwork();
        final String myNetwork = wallet.getParams().getId();
        if (!theirNetwork.equals(myNetwork)) {
            final String msg = String.format("Client is using '%s' and server is '%s'", theirNetwork, myNetwork);
            throw new ProtocolException(ProtocolException.Code.NETWORK_MISMATCH, msg);
        }
    }
View Full Code Here

                    file = f;
                    break;
                }
            }
            if (file == null)
                throw new ProtocolException("DOWNLOAD_CHUNK specified invalid file handle " + downloadChunk.getHandle());
            if (downloadChunk.getNumChunks() <= 0)
                throw new ProtocolException("DOWNLOAD_CHUNK: num_chunks must be >= 1");
            if (file.getPricePerChunk() > 0) {
                // How many chunks can the client afford with their current balance?
                PaymentChannelServerState state = payments == null ? null : payments.state();
                if (state == null)
                    throw new ProtocolException("Payment channel not initiated but this file is not free");
                long balance = state.getBestValueToMe().longValue();
                long affordableChunks = balance / file.getPricePerChunk();
                if (affordableChunks < downloadChunk.getNumChunks())
                    throw new ProtocolException("Insufficient payment received for requested amount of data: got " + balance);
                balance -= downloadChunk.getNumChunks();
            }
            for (int i = 0; i < downloadChunk.getNumChunks(); i++) {
                long chunkId = downloadChunk.getChunkId() + i;
                if (chunkId == 0)
                    log.info("{}: Starting download of {}", peerName, file.getFileName());
                // This is super inefficient.
                File diskFile = new File(directoryToServe, file.getFileName());
                FileInputStream fis = new FileInputStream(diskFile);
                final long offset = chunkId * CHUNK_SIZE;
                if (fis.skip(offset) != offset)
                    throw new IOException("Bogus seek");
                byte[] chunk = new byte[CHUNK_SIZE];
                final int bytesActuallyRead = fis.read(chunk);
                if (bytesActuallyRead < 0) {
                    log.debug("Reached EOF");
                } else if (bytesActuallyRead > 0 && bytesActuallyRead < chunk.length) {
                    chunk = Arrays.copyOf(chunk, bytesActuallyRead);
                }
                Payfile.PayFileMessage msg = Payfile.PayFileMessage.newBuilder()
                        .setType(Payfile.PayFileMessage.Type.DATA)
                        .setData(Payfile.Data.newBuilder()
                                .setChunkId(downloadChunk.getChunkId())
                                .setHandle(file.getHandle())
                                .setData(ByteString.copyFrom(chunk))
                                .build()
                        ).build();
                writeMessage(msg);
            }
        } catch (IOException e) {
            throw new ProtocolException("Error reading from disk: " + e.getMessage());
        }
    }
View Full Code Here

                break;
            case PAYMENT:
                handlePayment(msg.getPayment());
                break;
            default:
                throw new ProtocolException("Unhandled message");
        }
    }
View Full Code Here

            code = ProtocolException.Code.valueOf(error.getCode());
        } catch (IllegalArgumentException e) {
            log.error("{}: Unknown error code: {}", socket, error.getCode());
            code = ProtocolException.Code.GENERIC;
        }
        throw new ProtocolException(code, error.getExplanation());
    }
View Full Code Here

    private void handlePayment(ByteString payment) throws ProtocolException {
        try {
            Protos.TwoWayChannelMessage paymentMessage = Protos.TwoWayChannelMessage.parseFrom(payment);
            paymentChannelClient.receiveMessage(paymentMessage);
        } catch (InvalidProtocolBufferException e) {
            throw new ProtocolException("Could not parse payment message: " + e.getMessage());
        } catch (InsufficientMoneyException e) {
            // This shouldn't happen as we shouldn't try to open a channel larger than what we can afford.
            throw new RuntimeException(e);
        }
    }
View Full Code Here

TOP

Related Classes of net.plan99.payfile.Payfile$File$Builder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.