@Override public PetContract.Race reverseMap(Race output) { switch (output) { case CAT: return PetContract.Race.CAT; case DOG: return PetContract.Race.DOG; case PIG: return PetContract.Race.GUINEA_PIG; default: throw new EidIllegalArgumentException(new Eid("20161221:191805")); } }
private PetContract getPetByReference(String repr) { return entityReferenceMapper .map(repr) .flatMap(loadPetInteractor::findPetByReference) .orElseThrow( () -> new EidIndexOutOfBoundsException(new Eid("20180511:195020")) ); } }
private String getClassNameRepr(Serializable type) { if (type instanceof Class) { return classNameEncoder.getReprForClassName( Class.class.cast(type).getName() ); } throw new EidIllegalStateException( new Eid("20180509:143750"), "Unsupported type: %s", type.getClass() ); }
<I, T extends I> T newLazy(Class<I> cls, String eid) { try { return newLazyUnsafely(cls, eid); } catch (NoSuchMethodException | IllegalAccessException | InstantiationException ex) { throw new EidIllegalStateException("20180517:110633", ex); } }
private CharSequence pack(Serializable type, @Nullable Serializable id) { if (id == null) { return EMPTY_STRING; } String classNameRepr = getClassNameRepr(type); try { String repr = repr(id, classNameRepr); byte[] input = repr.getBytes(UTF_8); Cipher cipher = getEncrypt(); byte[] output; output = cipher.doFinal(input); return encode(output); } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | IllegalArgumentException ex) { throw new EidIllegalArgumentException("20140131:003856", ex); } }
@Override public Optional<PetContract> findPetByReference(EntityReference reference) { if (reference.getType() != Pet.class) { throw new SecurityException( new EidIllegalStateException(new Eid("20180511:212223")) ); } Optional<Pet> pet = petsGateway .findByReference(reference::getReference) .fetch(PetFetchProfile.WITH_OWNER); return pet.map(this::petAsContract); }
@SuppressWarnings("unchecked") private T unserialize(byte[] bytes) { try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) { return (T) in.readObject(); } catch (ClassNotFoundException | IOException ex) { throw new EidIllegalStateException("20161219:163039", ex); } } }
@Override public Race map(PetContract.Race input) { switch (input) { case CAT: return Race.CAT; case DOG: return Race.DOG; case GUINEA_PIG: return Race.PIG; default: throw new EidIllegalArgumentException(new Eid("20161221:183136")); } }
@Override @SuppressWarnings("unchecked") public <T> T create(Class<T> contractClass) { if (Predicate.class.isAssignableFrom(contractClass) && contractClass.isInterface()) { Class<? extends Predicate<InspectionPoint>> cls = (Class<? extends Predicate<InspectionPoint>>) contractClass; Predicate<InspectionPoint> predicate = createPredicateFalseProxy(cls); return (T) predicate; } throw new EidIndexOutOfBoundsException( new Eid("20180515:175613"), "Only interfaces extending Predicate can be instantiated by default, " + "provide your own implementation of BeanFactory" ); }
private EntityReference unpack(CharSequence repr) { try { Cipher cipher = getDecrypt(); byte[] input = decode(repr); String output = new String(cipher.doFinal(input), UTF_8); int idx = output.indexOf(REPR_SPLITTER); String serializedId = output.substring(0, idx); String type = output.substring(idx + REPR_SPLITTER.length()); String fqcn = getFqcnForType(type); return new EntityReferenceImpl( classLocator.locateClassByName(fqcn), serializer.unserialize(serializedId) ); } catch (GeneralSecurityException | ClassNotFoundException | NumberFormatException | IOException ex) { throw new EidIllegalArgumentException("20180509:134806", ex); } }
@RequestMapping(value = "/edit", method = RequestMethod.POST) public String edit(Model model, @ModelAttribute(FORM_OBJECT) EditPetForm form) { EntityReference reference = entityReferenceMapper .map(form.getReference()) .orElseThrow(() -> new EidIllegalStateException(new Eid("20180514:223428"))); PetContract pet = getPetFromForm(form.getData()); UpdatePetRequest request = new UpdatePetRequest( reference, pet ); Consumer<UpdatePetResponse> response = updatePetUseCase.updatePet(request); EditPetPresenter presenter = new EditPetPresenter( form.getReference(), pet, racePresenter ); response.accept(presenter); if (presenter.isSuccessful()) { return "redirect:../"; } else { return formHanding(model, presenter); } }
private byte[] decode(CharSequence charSequence) { try { return Base64 .getUrlDecoder() .decode(charSequence.toString()); } catch (IllegalArgumentException ex) { throw new EidIllegalStateException("20180509:132318", ex); } }
private byte[] serialize(T target) { try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { out.writeObject(target); out.flush(); return bos.toByteArray(); } catch (IOException e) { throw new EidIllegalStateException("20161219:162734", e); } }
private void initiate() { try { getEncrypt(); getDecrypt(); } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) { throw new EidIllegalStateException("20140131:003954", ex); } }
private <T extends Predicate<InspectionPoint>> T createPredicateFalseProxy( Class<T> contractClass) { InvocationHandler handler = (proxy, method, args) -> false; @SuppressWarnings("unchecked") Class<T> proxyClass = (Class<T>) java.lang.reflect.Proxy.getProxyClass( Thread.currentThread().getContextClassLoader(), contractClass ); try { return proxyClass .getConstructor(InvocationHandler.class) .newInstance(handler); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new EidIllegalStateException("20180515:180325", e); } } }
@Override public String serialize(Serializable serializable) { if (serializable instanceof Long) { return Long.toString((Long) serializable, RADIX); } try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { out.writeObject(serializable); out.flush(); return Base64.getUrlEncoder() .withoutPadding() .encodeToString(bos.toByteArray()); } catch (IOException e) { throw new EidIllegalStateException("20180509:143244", e); } }
private Cipher tryToInit(int cmode) throws NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher; try { cipher = getAesCipher(); cipher.init(cmode, getKey()); return cipher; } catch (InvalidKeyException ex) { mode = Mode.LIMITED; cipherInst = null; log.warn( "Using LIMITED cipher mode (AES 128 bits). Install Java Cryptography " + "Extension (JCE) Unlimited Strength Jurisdiction Policy Files for better " + "encryption (AES 256 bits)" ); try { cipher = getAesCipher(); cipher.init(cmode, getKey()); return cipher; } catch (InvalidKeyException ex1) { throw new EidIllegalStateException("20140131:003922", ex1); } } }