/** * To unmarshaled. * * @param <T> the generic type * @param aHttpBody the http body * @param aType the type * @return the t * @throws UnmarshalException the unmarshal exception */ @SuppressWarnings("unchecked") @Override public <T> T toUnmarshaled( String aHttpBody, Class<T> aType ) throws UnmarshalException { if ( String.class.isAssignableFrom( aType ) ) { return (T) aHttpBody; } throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + ">. This factory solely supports the type <String>." ); }
@SuppressWarnings("unchecked") @Override public <T> T toUnmarshaled( String aHttpBody, Class<T> aType ) throws UnmarshalException { try { if ( FormFields.class.equals( aType ) ) { return (T) FormFields.toFormFields( aHttpBody ); } if ( FormFields.class.isAssignableFrom( aType ) ) { FormFields theFormFields = (FormFields) aType.getConstructor().newInstance(); if ( aHttpBody != null && aHttpBody.length() != 0 ) { theFormFields.fromBodyFormFields( aHttpBody ); } return (T) theFormFields; } throw new UnmarshalException( "Cannot unmarshal HTTP Form-Fields \"" + aHttpBody + "\" to type <" + aType.getName() + "> as this factory only supports the type <" + FormFields.class.getName() + ">." ); } catch ( Exception e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + ">.", e ); } }
/** * {@inheritDoc} */ @Override public <T> T toUnmarshaled( String aHttpBody, Class<T> aType ) throws UnmarshalException { try { if ( aHttpBody == null ) { return null; } else if ( aHttpBody.length() == 0 ) { return aType.getConstructor().newInstance(); } T theFromJson = _mapper.readValue( aHttpBody, aType ); return theFromJson; } catch ( JsonMappingException | JsonParseException e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + "> at column <" + e.getLocation().getColumnNr() + ">.", e ); } catch ( Exception e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + ">.", e ); } }
/** * {@inheritDoc} */ @Override public <T> T toUnmarshaled( String aHttpBody, Class<T> aType ) throws UnmarshalException { try { if ( aHttpBody == null ) { return null; } else if ( aHttpBody.length() == 0 ) { return aType.getConstructor().newInstance(); } T theFromYaml = _mapper.readValue( aHttpBody, aType ); return theFromYaml; } catch ( JsonMappingException | JsonParseException e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + "> at column <" + e.getLocation().getColumnNr() + ">.", e ); } catch ( Exception e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + ">.", e ); } }
/** * This method creates / retrieves an instance of one of the given types * from the provided instance. The returned instance's type is to be tested * e.g. via "instanceof". * * @param aContext The context which may influence the object being created * by this factory. * @param aTypes The types identifying the instance to be created / * retrieved. * @return An instance of the type for which unmarshaling the context * succeeded. You have to verify yourself which type you actually * retrieved e.g. via "instanceof". * @throws UnmarshalException Thrown when unmarshaling / deserializing for * all provided types failed. */ default Object toUnmarshaled( SRC aContext, Class<?>... aTypes ) throws UnmarshalException { for ( Class<?> eType : aTypes ) { try { return toUnmarshaled( aContext, eType ); } catch ( UnmarshalException ignore ) { /* ignore */ } } throw new UnmarshalException( "Unable to unmarshal the context of type <" + aContext.getClass().getName() + "> to fit into one of the provided types." ); }
/** * This method creates / retrieves an instance of one of the given types * from the provided instance. The returned instance's type is to be tested * e.g. via "instanceof". * * @param aContext The context which may influence the object being created * by this factory. * @param aProperties The dynamic properties which are used to configure the * desired bean. * @param aTypes The types identifying the instance to be created / * retrieved. * @return An instance of the type for which unmarshaling the context * succeeded. You have to verify yourself which type you actually * retrieved e.g. via "instanceof". * @throws UnmarshalException Thrown when unmarshaling / deserializing for * all provided types failed. */ default Object toUnmarshaled( SRC aContext, Map<String, String> aProperties, Class<?>... aTypes ) throws UnmarshalException { for ( Class<?> eType : aTypes ) { try { return toUnmarshaled( aContext, aProperties, eType ); } catch ( UnmarshalException ignore ) { /* ignore */ } } throw new UnmarshalException( "Unable to unmarshal the context of type <" + aContext.getClass().getName() + "> to fit into one of the provided types." ); }
/** * {@inheritDoc} */ @Override public <T> T toUnmarshaled( String aHttpBody, Class<T> aType ) throws UnmarshalException { if ( aHttpBody == null ) { return null; } else if ( aHttpBody.length() == 0 ) { try { return aType.getConstructor().newInstance(); } catch ( Exception e ) { throw new UnmarshalException( "A problem occurred unmarshaling the HTTP body \"" + aHttpBody + "\" to type <" + aType.getName() + ">.", e ); } } JSONObject theJsonObj = XML.toJSONObject( aHttpBody ); String theJson = theJsonObj.toString( 4 ); if ( aType.isArray() ) { theJson = toUnboxed( theJson ); return super.toUnmarshaled( theJson, aType ); } else { return super.toUnmarshaled( theJson, aType ); } }
/** * This method expects an instance of (sub-)type {@link Map} of generic type * {@link String} (key) and {@link List} containing {@link String} instances * (value) as argument. Also supported is the derived {@link FormFields} * type. * * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public String toMarshaled( Object aObject ) throws MarshalException { try { if ( aObject instanceof FormFields ) { return ((FormFields) aObject).toBodyFormFields(); } else if ( aObject instanceof Map<?, ?> ) { FormFields theFormFields = new FormFieldsImpl(); theFormFields.putAll( (Map<String, List<String>>) aObject ); return theFormFields.toBodyFormFields(); } throw new UnmarshalException( "Cannot marshal HTTP Form-Fields from <" + aObject + "> to type <" + String.class.getName() + "> as this factory can only marshal HTTP Form-Fields of (sub-)type \"Map<String, List<String>\"." ); } catch ( Exception e ) { throw new MarshalException( "A problem occurred marshaling an object of type <" + aObject.getClass().getName() + ">.", e ); } }